Use Geany for Django projects

The following ideas may help you to better work on Django (www.djangoproject.com) projects with Geany.

Better syntax highlighting for Django templates

You can configure Geany to highlight variables and template tags in Django templates. This helps a lot to differentiate between normal HTML code and Django template code in the template files.

By default, highlighting of Django template tags is disabled because it may cause glitches with other special sorts of HTML code and not everyone is using the Django template language.

To enable that special highlight, you need to adjust the HTML filetype definition file. Either create or edit the file /home/<username>/.config/geany/filedefs/filetypes.html and add the following few lines (or just adjust the [lexer_properties] section if it already exists):1)

~/.config/geany/filedefs/filetypes.html
[lexer_properties]
lexer.html.django=1

More information about this file, its format and the path, e.g. on Windows, can be found in detail in the Geany manual, section Configuration Files.

Fine-tune styling

You can adjust the styling to some extend using the following settings which should be placed in the [styling] section of filetypes.html:

  • html_asp
  • python_identifier
  • python_default

Explanation:

{% load pages_tags i18n %}

{% and %} will be styled with html_asp, load, pages_tags and i18n will be styled with python_identifier and the spaces between with python_default.

Example:

~/.config/geany/filedefs/filetypes.html
[styling]
# foreground;background;bold;italic or named_style,bold,italic
html_asp=#ff0000
python_identifier=keyword_1,italic
python_default=default

The example above makes {% and %} styled in red (#ff0000) and the words in between will be styled with the named-style keyword_1 (which is by default dark blue) and renders it in italic.

For more information about configuring styles, please check the manual Geany manual, section Filetype configuration.

Useful Django snippets

Tomasz Karbownicki 2) created some useful snippets for the filetypes Python and HTML to assist you working on Django code and templates.

For convenience, the relevant parts are copied below to easily be inserted into your Geany snippet configuration file. To do so, just open the configuration file in Geany by using Tools → Configuration Files → snippets.conf and either paste the contents below completely or extend the already existing sections Python and HTML.

[Python]
# Django models
# by Tomasz Karbownicki <tomasz@karbownicki.com>
mclass=class %cursor%(models.Model):\n\t'''%cursor%'''\n\n\tdef __unicode__(self):\n\t\treturn self.XXXXX\n\n\tdef get_absolute_url(self):\n\t\treturn "/XXXXX/%s/" % self.slug\n\n\tclass Meta:\n\t\tverbose_name = "%cursor%"\n\t\tverbose_name_plural = "%cursor%"
mchar=%cursor% = models.CharField(max_length=50, verbose_name=u'%cursor%')
mint=%cursor% = models.IntegerField(verbose_name=u'%cursor%')
mtext=%cursor% = models.TextField(verbose_name=u'%cursor%')
mkey=%cursor% = models.ForeignKey(%cursor%, verbose_name=u'%cursor%')
mimage=%cursor% = models.ImageField(upload_to='', verbose_name=u'%cursor%')
mbool=%cursor% = models.BooleanField(verbose_name=u'%cursor%')
mdate=%cursor% = models.DateField(verbose_name=u'%cursor%', help_text='Format daty: 2009-04-28')
memail=%cursor% = models.EmailField(verbose_name=u'%cursor%')
murl=%cursor% = models.URLField(verbose_name=u'%cursor%')
mslug=%cursor% = models.SlugField(verbose_name=u'%cursor%', unique=True)
 
[HTML]
# Django templates
if={% if %cursor% %}\n\t\n{% endif %}
for={% for sth in %cursor% %}\n\t%cursor%\n{% endfor %}
dv={{ %cursor% }}
db={% %cursor% %}
dbl={% block %cursor% %}\n\t%cursor%\n{% endblock %}
trans={% trans "%cursor%" %}
com={# %cursor% #}
comm={% comment%}
ecomm={% endcomment%}

More snippets for Python, HTML and many other languages can be found in this wiki, see Snippets.

More information about snippets in Geany can be found in detail in the Geany manual, section Snippets.

Tags

Tags for django (version 1.4.1) can be found here. For information on using tag files, see the Tags section of the Geany User Manual.

Live Preview in Geany

Using the great Web helper plugin (from the Geany-Plugins package) you can view and test your site directly in Geany without switching to an external browser.

Simply enable the Web helper plugin in Geany's plugin manager dialog, open it preferences dialog and enable the option “Browser auto reload” (to automatically reload the page when you save a file in Geany).

Then you will get a new tab “Web preview” in the left message window (or whereever you configured the plugin to show up) and you can enter any weg page address, e.g. those of your currently edited Django project. Below is a screenshot demonstrating the plugin: the opened web page on the left, the template code on the right and in the terminal window at the bottom, Django's runserver command is running:

This way you can edit your Django and template code in Geany as usual and have it automatically shown as web page in the Web helper plugin without the need of switching to an external browser, and the Django debug server can also be ran in Geany, using the embedded terminal.

Twig/Symfony2 Support

If you want to add detection/highlighting of Twig/Symfony2 files (based on Django's templates) when opening in Geany, add *.html.twig to the HTML key in the filetype_extensions.conf file. For more information on filetype_extensions.conf, see the Filetype extensions section of the Geany User Manual.

Print/export