Django:url conf问题

时间:2013-07-20 14:52:07

标签: django urlconf

我正在尝试将我的项目更改为使用基于类的视图,并且我的url conf遇到了一个奇怪的错误

我有一个基于分类的观点:

class GroupOrTeamCreate(AjaxableResponseMixin, CreateView):
    model = GroupOrTeam
    fields = ['name', 'description']
    #success_url = reverse('home_page')  # redirect to self

我最后一行被注释掉了,因为如果我不这样做,django会抱怨我的网址中没有模式。

首先,这是我的基础urls.py

urlpatterns = patterns('',
    url(r'^$', TemplateView.as_view(template_name='core/home.html'), name='home_page'),
    url(r'^administration/', include('administration.urls', app_name='administration')),
    url(r'^reports/', include('reports.urls', app_name='reports')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

显然,那里有模式。如果我注释掉管理网址,它就可以了。所以我认为问题存在于某个地方。

管理urls.py

from django.conf.urls import patterns, url
from .views import ActiveTabTemplateView, GroupOrTeamCreate, GroupOrTeamUpdate


urlpatterns = patterns('',

    # Add page
    url(r'^add/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'), name='add_page'),
    url(r'^add/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'),
    name='add_page'),

    # Seach page
    url(r'^search/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'), name='search_page'),
    url(r'^search/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'),
    name='search_page'),

    #--------------------------------------------------------------------------
    # Forms
    #--------------------------------------------------------------------------

    # Groups and teams
    url(r'^group-or-team-form/$', GroupOrTeamCreate.as_view(template_name='administration/forms/groups_form.html'),
        name='group_or_team_form'),
    url(r'^group-or-team-form/(?P<pk>\d+)/$',
        GroupOrTeamUpdate.as_view(template_name='administration/forms/groups_form.html'),
        name='group_or_team_form'),

)

我没有看到问题..这些页面在没有反向语句的情况下加载就好了,它似乎是反向语句的引入打破了一切,但我不能为我的生活弄清楚什么是原因是。

1 个答案:

答案 0 :(得分:0)

您收到错误,因为在定义类时尚未加载URL conf。

使用reverse_lazy代替reverse

from django.core.urlresolvers import reverse_lazy

success_url = reverse_lazy('home_page')