我在使用基于类的视图和reverse_lazy时遇到了一些奇怪的问题。
调用网站时出现以下错误:
ImproperlyConfigured at /dashboard/student/
The included urlconf core.urls doesn't have any patterns in it
我的views.py:
class DashStudentMain(TemplateView):
model_class = None
template_name = 'learn/dashboard/snip_student_1.html'
tab_list = {
("Main", reverse_lazy('dash_student_main_url')),
#("History", reverse_lazy('dash_student_main_url'))
}
active_tab = "Main"
My core.urls:
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
from django.views.generic import RedirectView
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns(
'',
url(r'^$', 'core.views.home', name='home_url'),
url(r'^home', 'core.views.home'),
url(r'^dashboard/', include('tc_learn.dashboard.urls')),
...
)
我的tc_learn.dashboard.urls:
from django.conf.urls.defaults import patterns, url
from .views import DashStudentMain, DashStudentHistory
urlpatterns = patterns(
# Student + Tabs
url(r"^", DashStudentMain.as_view()),
url(r"^student/$", DashStudentMain.as_view(), name="dash_student_main_url"),
url(r"^student/history/$", DashStudentHistory.as_view(), name="dash_student_history_url"),
我已经
了知道我可能缺少什么吗?谢谢!
编辑:
看起来这个问题来自tab_list对象。
当我通过tab_list = reverse_lazy('dash_student_main_url')
直接分配对象时,代码工作正常。当我在列表中使用它时,它显示错误。有没有人知道这种情况的解决方法?
答案 0 :(得分:9)
更改此代码:
tab_list = {
("Main", reverse_lazy('dash_student_main_url')),
#("History", reverse_lazy('dash_student_main_url'))
}
为:
tab_list = [
("Main", reverse_lazy('dash_student_main_url')),
#("History", reverse_lazy('dash_student_main_url'))
]
与您给变量的名称相反,您没有创建列表,而是set。元素在集创建时立即进行评估,因为集合需要更多地了解它们包含的值。将其更改为正确的列表将允许按照预期懒惰地评估元素。
答案 1 :(得分:2)
在tc_learn.dashboard.urls中:您缺少第一个参数(在您的情况下为空前缀)。将其更改为:
urlpatterns = patterns(
'',
url(r"^", DashStudentMain.as_view()),
url(r"^student/$", DashStudentMain.as_view(), name="dash_student_main_url"),
url(r"^student/history/$", DashStudentHistory.as_view(), name="dash_student_history_url"),
)
此外,如果你想让它代表一个空的,那么第一个正则表达式应该是r“^ $” 看看它是否有效。让我知道!