NoReverseMatch位于/ <url> </url>

时间:2015-04-15 01:25:35

标签: django

Django 1.7.0 beta 4
Python 2.7.9

我目前正在尝试使用Django的CBV来创建模态。我对ListView没有任何问题,但是在尝试使用url标签调用UpdateView时我遇到了困难。 我收到以下错误:

  

/ dashboard_usuario_list /
的NoReverseMatch   使用参数'(u'test2',)'和关键字参数'{}'找不到'dashboard_usuario_edit'的反转。尝试了1种模式:['dashboard_usuario_edit /(?Pd +)/?$']

views.py:

class UserProfileListView(ListView):
    model = UserProfile
    template_name = 'dashboard_usuario_list.html'
    def get_queryset(self):
        return UserProfile.objects.all()

class UserProfileUpdateView(UpdateView):
    model = UserProfile
    form_class = UserProfileForm
    template_name = 'dashboard_usuario_edit.html'

    def dispatch(self, *args, **kwargs):
        self.username = kwargs['username']
        return super(UserProfileUpdateView, self).dispatch(*args, **kwargs)

    def form_valid(self, form):
        """
        If the form is valid, redirect to the supplied URL.
        """
        form.save()
        user_profile = UserProfile.objects.get(username = self.username)
        return HttpResponse(render_to_string('dashboard_usuario.html', {'username':user_profile}))


    def get_context_data(self, **kwargs):
        context = super(UserProfileUpdateView, self).get_context_data(**kwargs)
        return context

url.py:

url(r'^dashboard_usuario_list/', views.UserProfileListView.as_view(), name = 'dashboard_usuario_list'),
url(r'^dashboard_usuario_edit/(?P<username>d+)/?$', views.UserProfileUpdateView.as_view(), name = 'dashboard_usuario_edit'),

dashboard_usuario_list.html:
<a href="{% url 'dashboard_usuario_edit' user.user.username %}"></a>

提前致谢!

1 个答案:

答案 0 :(得分:2)

dashboard_usuario_edit的网址具有正则表达式模式,只接受整数\d+username,这是错误的,用户名可以包含字母数字字符,因此您的模式应为:

url(r'^dashboard_usuario_edit/(?P<username>[\w.@+-]+)/?$', views.UserProfileUpdateView.as_view(), name = 'dashboard_usuario_edit'),