With the increase in the number of projects, the problem of automating the creation of a framework for new applications arose. Until recently, we created new projects by copying the prepared template project and changing various settings in different places of the project. Of course it killed a lot of time.
Attempts to solve this problem brought me to a project called
Python Paste . In general, this is a set of utilities for creating web applications, for example, you can create your own framework (in
Pylons they use it). In addition to web-based utilities, Python Paste contains a Paste Script module, with which you can create templates and create projects based on them.
How does all this work?
- Create a project that will contain blanks, indicate the path to the catalog with blanks and make other settings.
- In the template directory, we create a file structure, taking into account that files and directory names will be passed through the template Cheetah (you can use various variables inside the files, such as the name of the project and any others as you like).
- We install our project containing blanks.
- The generation team creates a new project based on the template. Before generation, they will be asked for the project name and various parameters that were specified in the template settings.
Create a simple template for projects on Django
Install PasteScript
pip install PasteScript
,
We create something like this project structure:
')

Now we define the location of the templates and settings for them:
from setuptools import setup, find_packages
setup (
name = 'paster-templates' ,
version = "0.1" ,
packages = find_packages ( ) ,
install_requires = [
'setuptools' ,
'PasteScript' ,
'Cheetah' ,
] ,
include_package_data = True ,
zip_safe = False ,
# Here we describe our templates.
entry_points = "" "
[paste.paster_create_template]
simple = templates.pastertemplates: SimpleTemplate
"" "
)
where simple is the name of the template, SimpleTemplate is a class with settings.
In the SimpleTemplate class, we set up the template itself, specify the directory with the files, a brief description and the “base name” parameter, which we ask the user to enter:
from paste. script . templates import template
from paste. script . templates import var
class SimpleTemplate ( Template ) :
_template_dir = 'simple'
summary = "Simple project"
use_cheetah = true
vars = [
var ( 'database' ,
'Database name.' ,
default = "test_db" ) ,
]
Let's go into templates / and generate a new jung project
django-admin.py startproject simple
which will be the template.
Go to setting the template files. The parameters specified during the generation can be used inside the template files by adding to the file name _tmpl. These variables are of the form
$(property_name)
, the names of the directories for passing through the template engine are:
+property_name+
. For example, a piece of the settings.py_tmpl file:
...
# Project name - $ (project)
DATABASES = {
'default' : {
'ENGINE' : 'django.db.backends.sqlite3' ,
'NAME' : '$ (database)' ,
}
}
...
And a couple of comments on the template files:
- If we write in Russian in the template, the
#encoding UTF-8
should be inserted in the beginning of the file. - If in the template you need to use the $ character and so that cheetah does not take it as your own, you need to wrap it in
#raw ... $content... #end raw
Conclusion
That's all. Install the
python setup.py install
package
python setup.py install
paster create --list-templates
we look at the list of templates in the system (as far as I understand, templates are searched for using the scan of egg's installed in the system for template plug-ins that were described in entry_points).
paster create --template=simple SimpleProject
command
paster create --template=simple SimpleProject
we create a new project based on the simple template.
It remains to create templates for all occasions.
Project sources on
GitHubReferences: