Pergunta

I'm trying to change my project to use class based views, and I'm running into a strange error with my url conf

I have a classed based view:

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

I have the last line commented out because if I don't, django complains that there are no patterns in my url conf.

To start, here's my base 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)),
)

Clearly there are patterns in there. If I comment out the administration urls, it works. So I assume the problem is in there somewhere.

administration 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'),

)

I'm not seeing the problem.. these pages load just fine without that reverse statement in, it appears to be the introduction of the reverse statement that breaks everything but I can't for the life of me work out what the cause is.

Foi útil?

Solução

You get the error because the URL conf has not been loaded yet when the class is defined.

Use reverse_lazy instead of reverse.

from django.core.urlresolvers import reverse_lazy

success_url = reverse_lazy('home_page')
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top