Creating Project Templates

Posted by shanec on 2007-01-21 16:48
OS: All / Any | Product: Komodo | tags: komodo project template django
Question: 

Why would I want to create a project template?

Answer: 

In my quest to create some screen shots of Komodo I decided to use Django for the sample application. Since I've never looked at Django, I figured I'd also make a project template at the same time.

Why would I want a project template? Well, the template system provides a way for me to have repetitive functionality for a given type of project. I can setup macros, run commands, snippets, etc. that are specific to the type of project I want to create. Then they are always available whenever I create the new project. I can also have the project initialize some basic items when it is created.

So here is what I did:

1. Download and install Django (from www.djangoproject.com). I followed the install instructions at http://www.djangoproject.com/documentation/install/. I also had to symlink django-admin.py into my /usr/local/bin directory.

2. Create a new project called "djangotemplate".

3. Create a folder called "project", though it can be whatever name you would like.

4. Create a macro in the project folder called "oncreate". This is a special name for project templates. When you create a new project from a template, if this macro is in the project, it gets executed right away.

In this macro I have the following JavaScript code:

var project = komodo.macro.project;
var name = /^(.*?;)(\.kpf)?;$/(project.name)[1];
var startproject = project.getChildByAttributeValue('name','startproject', 1);
Run_RunCommand(window, "django-admin.py startproject "+name, "%p", null,
               false, false, true, "command-output-window");

This code runs the django-admin.py script when the project is created. You will then see a folder in your new project that has the same name as the project file itself. This contains the base scripts for your new Django project.

5. I then created a new macro for the template, called "runserver". This macro contains the following code:

var project = komodo.macro.project;
var name = /^(.*?;)(\.kpf)?;$/(project.name)[1];
Run_RunCommand(window, "%(python) manage.py runserver", "%p/"+name, null,
               false, false, true, "new-console");

This macro basically executes the built in Django webserver.

6. Now you need to save the project as a template. Go to the Project menu, and choose to "Create Template from Project". Call it Django.

7. Now you want to test your template. Go to the File->New menu, and choose "New Project from Template". Select a directory and name for the project and click OK.

There you go, a new Django project. You can use the runserver macro to start the server, and test the basic setup with your web browser.

Using these simple steps, you've created some new functionality for Komodo that you can use over and over. Try adding more Django commands to the template and see what you can come up with!

NOTE: I've attached a kpz file that you can either import into the toolbox, or drop into the following locations:

OSX:
~/Library/Application Support/Komodo/4.0/project-templates/My Templates/django.kpz

Linux:
~/.komodo/4.0/project-templates/My Templates/django.kpz

Windows:
%APPDATA%/ActiveState/Komodo/4.0/project-templates/My Templates/django.kpz

AttachmentSize
django.kpz1.6 KB

jvh | Fri, 2007-12-07 20:45

This works great however there are two things I'm puzzled by:

1. I have all of my django projects located in a folder called "projects" in my home directory (on linux). When I create a new komodo project using the project template, my new django project is created via django-admin but the komodo project automatically includes all other django projects I have in the directory. I would much rather prefer to only see the files associated with the django project I created via the komodo project template as I only intend to work on one django project at a time. I was hoping that removing the other files from the project in komodo and then saving would resolve this but no...the next time I open it - there they are.

2. The macros "oncreate" and "runserver" are definitely handy but they are located in a folder called "project" in my komodo project...it would make far more sense to have these macros available under the actual django project folder. Is this possible? If so, can you lend a hand as to how this would be accomplished on creation using the django project template?

jvh

toddw
ActiveState Staff
Mon, 2007-12-10 12:18

The project created from the template is a live project, which means by default it will automatically include all the files/folders located in the project's start directory.

1. You can change the imported settings by right-clicking on the project and selecting "Properties" and changing the "Directory Import" settings to actually reference the django folder that the oncreate macro (django-admin) created.

2. You can simply drag/drop the oncreate and runserver macros to make them reside in the root of the project. You can also make these changes to the project template itself, simply unzip the "project.kpf" file from the "django.kpz" arachive, open up the project.kpf in Komodo and make the changes you require and save the project, then repack project.kpf into the django.kpz file.

Cheers,
Todd

jvh | Tue, 2007-12-11 09:00

Thanks, works great.

jvh

spkane | Sat, 2008-11-15 16:34

For Komodo 5.x the path seems to be:

~/Library/Application Support/KomodoIDE/5.0/project-templates/My Templates

on OS X and I'm sure something similar of the other OSes. Note you may need to create the project-templates/My Templates directories.