我想使用Django1.11制作注册表单。


urls.py

app_name = 'accounts'

urlpatterns = [
    url(r'^create/$', views.SignUpView.as_view(), name='create'),
    url(r'^check/$', views.CheckView.as_view(), name='check'),
    url(r'^update/$', views.CorrectView.as_view(), name='update'),
    # ...
]


views.py

class SignUpView(CreateView):
    model = User
    form_class = UserForm
    template_name = "accounts/create.html"

    def get_success_url(self):
        check_view = CheckView.as_view()
        return redirect(check_view, pk=self.object.pk)

class CheckView(DetailView):
    model = User
    template_name = "accounts/check.html"

class CorrectView(UpdateView):
    model = User
    form_class = UserForm
    template_name = "accounts/create.html"

    def get_success_url(self):
        check_view = CheckView.as_view()
        return redirect(check_view, pk=self.object.pk)



新用户在SignUpView(generic.CreateView)中输入信息后,他将看到自己刚刚在CheckView(generic.DetailView)中输入的内容,如果他发现自己犯了一些错误,则可以在CorrectView(generic)中重新输入信息。 .UpdateView)。
例如,我不想使用url r'^check/(?P<pk>[0-9]+)$'。这是因为,例如,如果用户在浏览器中输入URL .../check/1,不幸的是他可以看到其他人的信息。


当我运行上面的代码时,发生错误Reverse for 'accounts.views.CheckView' not found. 'accounts.views.CheckView' is not a valid view function or pattern name.。请告诉我如何在没有url包含pk的情况下重定向到CheckView(generic.DetailView)。

最佳答案

您可以将网址的结构更改为不使用slug,例如:

# Url dell'app accounts.
url(r'^accounts/register/$', RegistrationView.as_view(form_class=CustomUserForm), name='registration-register'),
url(r'^accounts/profile/$', UserProfileView.as_view(), name='user-profile'),
url(
    r'^accounts/profile/(?P<company>[-\w]+)/modifica/$',
    UpdateCompanyView.as_view(),
    name='update-company-view-profile'
),
url(
    r'^accounts/change-password/$',
    password_change, {'post_change_redirect': 'user-profile'}, name='password_change'
),
url(r'^accounts/update/$', UserProfileUpdateView.as_view(), name='user-profile-update'),
url(r'^accounts/', include('registration.backends.hmac.urls')),


这是我在项目中使用的网址结构。
那么我就可以操纵用户,或仅通过使用request.user从中获取信息!

10-06 10:25
查看更多