我有一些已在我的基本模板中使用的命名网址,如下所示:
<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>
这很好用。但是,我在主urls.py中定义了所有项目的URL 模块,所以我试图分解每个模块的使用包括。
主:
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)),
)
应用:
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'),
)
由于我进行了这些更改,因此出现以下错误:
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</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.'
如果我删除模板中指定网址的引用并手动输入,它们就可以正常工作。
我做错了什么?
任何建议表示赞赏。
感谢。
答案 0 :(得分:3)
尝试删除额外的'。'来自lessons.urls第8行的字符,位于'addstudent'前面。
Django正在尝试将'lessons.views..addstudent'解析为模块+方法,并且额外的时间段会导致关键错误。