4

我在使用基于类的视图和 reverse_lazy 时遇到了一些奇怪的问题。

调用网站时出现以下错误:

ImproperlyConfigured at /dashboard/student/
The included urlconf core.urls doesn't have any patterns in it

我的意见.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"

我的核心网址:

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"),

我有

  • 重新启动服务器,以确保正确加载 url
  • 注释掉("Main", reverse_lazy('dash_student_main_url'))以确保 urls.py 语法正常
  • 删除了url(r"^", DashStudentMain.as_view()) 行,因为它无论如何都没有使用,但是没有它 /dashboard/student 根本不起作用..

知道我可能会错过什么吗?谢谢!

编辑:看起来问题来自 tab_list 对象。当我通过 直接分配对象时tab_list = reverse_lazy('dash_student_main_url'),代码工作正常。当我在列表中使用它时,它会显示该错误。有谁知道这种情况的解决方法?

4

2 回答 2

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。元素在集合创建时立即被评估,因为集合需要更多地了解它们包含的值。将其更改为适当的列表将允许按预期懒惰地评估元素。

于 2013-10-07T17:12:10.507 回答
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"^$" 如果你想让它代表一个空的并且看看它是否有效。让我知道!

于 2013-10-02T17:27:21.257 回答