开发者

Django - Named URLs not working in template

I have a number of named urls which I have been using in my base template like so:

<a href="{% url addstudent %}">Add Student</a>
<a href="{% url addlocation %}">Add Location</a>
<a href="{% url addservice %}">Add Service</a>
<a href="{% url addlesson %}">Add Lesson</a>
<a href="{% url mystudents %}">My Students</a>
<a href="{% url mylessons %}">My Lessons</a>

This was working fine. However, I had all of my project's URLs defined in the main urls.py module, so I tried to break it up an use includes for each module.

Main:

from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^lessons/', include('lessons.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

App:

from django.conf.urls.defaults import patterns, include, url
from django.views.generic import DetailView, ListView
from lessons.views import StudentListView, LessonListView
from django.contrib.auth.decorators import login_required

urlpatterns = patterns('lessons.views',
    url(r'^addlesson/$', 'addlesson', name='addlesson'),
    url(r'^mylessons/$', login_required(LessonListView.as_view()), name='mylessons'),
    url(r'^addstudent/$', '.addstudent', name='addstudent'),
    url(r'^mystudents/$', login_required(StudentListView.as_view()), name='mystudents'),
    url(r'^addservice/$', 'addservice', name='addservice'),
    url(r'^addlocation/$', 'addlocation', name='addlocation'),
)

Since I made these changes, I get the following error:

Caught KeyError while rendering: 'lessons.views.'

    Environment:


Request Method: GET
Request URL: http://localhost:8000/lessons/addlesson/

Django Version: 1.3
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.flatpages',
 'registration',
 'lessons',
 'utilities',
 'django.contrib.admin',
 'debug_toolbar',
 'south']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')


Template error:
In template /home/dan/mylessons/templates/base.html, error at line 93
   Caught KeyError while rendering: 'lessons.views.'
   83 :         <script>


   84 :     $(function() {


   85 :         $(".datetimepicker").datetimepicker();


   86 :     });


   87 :     </script>


   88 :   </head> 


   89 :   <body> 


   90 :         <div id="wrapper"> 


   91 :             <div id="header"> 


   92 :     


   93 :             <a href="{% url addlocation %}">Add Location</a>


   94 :             <a href="{% url addservice %}">Add Service</a>


   95 :             <a href="{% url addlesson %}">Add Lesson</a>


   96 :             <a href="{% url mystudents %}">My Students&l开发者_如何学Got;/a>


   97 :             <a href="{% url mylessons %}">My Lessons</a>


   98 :             


   99 :             


   100 :                


   101 :  


   102 :  


   103 :  


Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)
File "/home/dan/mylessons/utilities/decorators.py" in wrapper
  13.             return f(request, *args, **kwargs)
File "/home/dan/mylessons/lessons/views.py" in addlesson
  104.     }, context_instance=RequestContext(request))
File "/usr/local/lib/python2.6/dist-packages/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in render_to_string
  188.         return t.render(context_instance)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  123.             return self._render(context)
File "/usr/local/lib/python2.6/dist-packages/django/test/utils.py" in instrumented_test_render
  57.     return self.nodelist.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/usr/local/lib/python2.6/dist-packages/django/test/utils.py" in instrumented_test_render
  57.     return self.nodelist.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/defaulttags.py" in render
  437.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in reverse
  391.             *args, **kwargs)))
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in reverse
  312.         possibilities = self.reverse_dict.getlist(lookup_view)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in _get_reverse_dict
  229.             self._populate()
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in _populate
  208.                     for name in pattern.reverse_dict:
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in _get_reverse_dict
  229.             self._populate()
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in _populate
  208.                     for name in pattern.reverse_dict:
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in _get_reverse_dict
  229.             self._populate()
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in _populate
  220.                 lookups.appendlist(pattern.callback, (bits, p_pattern))
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in _get_callback
  164.             self._callback = get_callable(self._callback_str)
File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py" in wrapper
  124.         result = func(*args)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in get_callable
  91.                 lookup_view = getattr(import_module(mod_name), func_name)
File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py" in import_module
  36.     return sys.modules[name]

Exception Type: TemplateSyntaxError at /lessons/addlesson/
Exception Value: Caught KeyError while rendering: 'lessons.views.'

If I remove the reference to the named urls in the template and type them in manually, they work fine.

What am I doing wrong?

Any advice appreciated.

Thanks.


Try removing the extra '.' character from line 8 of lessons.urls, in front of 'addstudent'.

Django is trying to resolve 'lessons.views..addstudent' into a module+method, and the extra period is leading to a key error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜