Estoy tratando de utilizar la función de Django reverse() en la definición de la forma de Django para mi widget personalizado, pero estoy recibiendo un error:Usando inversa() en Django forma
ImproperlyConfigured
The included urlconf urls doesn't have any patterns in it
Aquí está el código:
class WorkForm(forms.Form):
# ...
category = forms.ChoiceField(
required=True,
label=_('Category'),
help_text=_('Select most appropriate category for your work.')
)
subcategory = forms.ChoiceField(
widget=DependantChoiceWidget(
default_value=_('Select category first'),
data_source_url=reverse('works-json-categories'),
# data_source_url='', -- it works this way
depends_on='category_id'
),
required=True,
label=_('SubCategory'),
help_text=_('Which subcategory suits your work best.')
)
Estoy bastante seguro de que mi 'works.urls' está configurado correctamente, ya que todas las demás páginas funcionan como se esperaba.
¿Hay alguna razón por la que no puedo usar reverse()
en la definición de formulario? ¿Tiene algo que ver con cuándo se ejecuta este código? ¿Hay alguna manera de solucionar esto, o la única opción aquí es codificar la URL?
Aquí está lleno de volcado de error:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/works/add?category=1&subcategory=1
Django Version: 1.4 pre-alpha
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'social_auth',
'sorl.thumbnail',
'helpers',
'users',
'works',
'debug_toolbar']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
89. response = middleware_method(request)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in process_request
67. if (not _is_valid_path(request.path_info, urlconf) and
File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in _is_valid_path
164. urlresolvers.resolve(path, urlconf)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
416. return get_resolver(urlconf).resolve(path)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
298. for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
328. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
323. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/var/www/megenius/trunk/urls.py" in <module>
27. url(r'^works/', include('works.urls')),
File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include
24. urlconf_module = import_module(urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/var/www/megenius/trunk/works/urls.py" in <module>
2. from works.views import *
File "/var/www/megenius/trunk/works/views.py" in <module>
9. from works.forms import WorkForm
File "/var/www/megenius/trunk/works/forms.py" in <module>
10. class WorkForm(forms.Form):
File "/var/www/megenius/trunk/works/forms.py" in WorkForm
31. data_source_url=reverse('works-json-categories'),
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in reverse
473. (prefix, resolver.reverse(view, *args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in reverse
360. possibilities = self.reverse_dict.getlist(lookup_view)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in reverse_dict
276. self._populate()
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _populate
242. for pattern in reversed(self.url_patterns):
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
332. raise ImproperlyConfigured("The included urlconf %s doesn't have any patterns in it" % self.urlconf_name)
Exception Type: ImproperlyConfigured at /works/add
Exception Value: The included urlconf urls doesn't have any patterns in it
¿No debería su cadena inversa tener guiones bajos en lugar de guiones? – jcfollower
Si se llaman patrones de url, los nombres pueden contener guiones. Habiendo dicho eso, prefiero usar guiones bajos en mis nombres de patrones de url. – Alasdair