Opened 16 years ago

Closed 16 years ago

#9941 closed (duplicate)

{% url ... %} template tag may fail when not using a settings module

Reported by: Simon Sapin Owned by: nobody
Component: Template system Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When using django.conf.settings.configure(...) instead of a settings module as documented, settings.SETTINGS_MODULE is set to None.

The {% url ... %} template tag is implemented as following, in django.template.defaulttags.URLNode.render :

        # Try to look up the URL twice: once given the view name, and again
        # relative to what we guess is the "main" app. If they both fail, 
        # re-raise the NoReverseMatch unless we're using the 
        # {% url ... as var %} construct in which cause return nothing.
        url = ''
        try:
            url = reverse(self.view_name, args=args, kwargs=kwargs)
        except NoReverseMatch:
            project_name = settings.SETTINGS_MODULE.split('.')[0]
            try:
                url = reverse(project_name + '.' + self.view_name,
                              args=args, kwargs=kwargs)
            except NoReverseMatch:
                if self.asvar is None:
                    raise

In some cases (for example in the admin templates), it will execute this : settings.SETTINGS_MODULE.split('.') which raises AttributeError: 'NoneType' object has no attribute 'split'.
Instead, it should check for settings.SETTINGS_MODULE being None before using it.

As a workaround, set SETTINGS_MODULE to some module name. This module needs to exist, as it is imported by other parts of Django, but does not need to actually contain the settings (which are set with django.conf.settings.configure)

Change History (2)

comment:1 by Simon Sapin, 16 years ago

The described workaround is bad as it breaks django.core.management.execute_from_command_line (ie manage.py) by causing it to raise RuntimeError: Settings already configured.

Instead, i added the following to my url config, so that {% url django-admindocs-docroot as docroot %} wouldn't fail in the admin :

(r'^admin/doc/', include('django.contrib.admindocs.urls')),


comment:2 by Jacob, 16 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #9005

Note: See TracTickets for help on using tickets.
Back to Top