Django:在URL中没有pk的情况下重定向到DetailView

时间:2017-12-05 00:55:20

标签: python django redirect django-class-based-views

我想用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(通用) .UpdateView)。

我不想以网址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 include pk的情况下重定向到CheckView(generic.DetailView)。

1 个答案:

答案 0 :(得分:1)

您可以将网址结构更改为不使用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!

从中获取信息