Code

Opened 6 years ago

Closed 5 years ago

#9941 closed (duplicate)

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

Reported by: Eftarjin 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: UI/UX:

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)

Attachments (0)

Change History (2)

comment:1 Changed 6 years ago by Eftarjin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 5 years ago by jacob

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #9005

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.