gpt4 book ai didi

python - Django:重定向到 DetailView,URL 中没有 pk

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:47 26 4
gpt4 key购买 nike

我想用 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.DetailView) 中重新输入信息.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.
发生。请告诉我如何重定向到 CheckView(generic.DetailView) 而不包含 url include pk。

最佳答案

您可以更改网址结构以不使用 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 从中获取信息!

关于python - Django:重定向到 DetailView,URL 中没有 pk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47644418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com