Django使用UpdateView获取此错误:NoReverseMatch at / user / 2 / edit

时间:2017-09-10 20:35:42

标签: django django-views django-urls

我有更新视图供用户更新其个人资料。但是我一直收到这个错误:

NoReverseMatch at /user/2/edit
Reverse for 'user_profile' with no arguments not found. 1 pattern(s) tried: ['user/(?P<pk>\\d+)/$']

我没有尝试过任何工作(添加pk等)。

UpdateView:

class UserEditProfileView(LoginRequiredMixin,UpdateView):
    login_url = '/login/'
    model = UserProfile
    fields = [
            'first_name',
            'profile_pic',
            'location',
            'title',
            'user_type',
            'website',
            'about',
            'twitter',
            'dribbble',
            'github'
            ]
    template_name_suffix = '_edit_form'
    success_url = reverse_lazy('users:user_profile')

应用网址:

from django.conf.urls import url
from users import views

app_name = 'users'

urlpatterns = [
    url(r'^$',views.UserListView.as_view(),name='user_list'),
    url(r'^(?P<pk>\d+)/$',views.detailprofile,name='user_profile'),
    url(r'^(?P<pk>\d+)/edit$',views.UserEditProfileView.as_view(),name='user_profile_edit'),
    url(r'^login/$',views.user_login,name='user_login'),
    url(r'^logout/$',views.user_logout,name='user_logout'),
    url(r'^register/$',views.register,name='register'),
]

修改个人资料模板:

{% extends "users/base.html" %}

{% block content %}

    <div class="form-base">
        <h2>Edit Profile</h2>
        <form method="POST">
            {% csrf_token %}
            {{ form.as_p }}
            <input type="submit" value="Save" />
        </form>
</div>

{% endblock %}

如果您需要查看其他内容,请与我们联系。

1 个答案:

答案 0 :(得分:1)

您还需要在pk

中提供user_profile
class UserEditProfileView(LoginRequiredMixin,UpdateView):
    login_url = '/login/'
    model = UserProfile
    fields = [
        'first_name',
        'profile_pic',
        'location',
        'title',
        'user_type',
        'website',
        'about',
        'twitter',
        'dribbble',
        'github'
        ]
    template_name_suffix = '_edit_form'

    def get_success_url(self):
        userid = self.kwargs['pk']
        return reverse_lazy('users:user_profile', kwargs={'pk': userid})