Home Django: redirect to DetailView without pk in URL
Reply: 1

Django: redirect to DetailView without pk in URL

Zuya
1#
Zuya Published in 2017-12-05 00:55:19Z

I want to make sign-up forms with 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)

After a new user inputs his information in SignUpView(generic.CreateView), he will see what he just has input in CheckView(generic.DetailView), and if he notice that he has made some mistakes, he reinput his information in CorrectView(generic.UpdateView).

I want not to make url r'^check/(?P<pk>[0-9]+)$' for example. This is because if an user inputs a URL .../check/1 for example in browser, unfortunately he can see the information of another person.

When I run the code above, the error Reverse for 'accounts.views.CheckView' not found. 'accounts.views.CheckView' is not a valid view function or pattern name. is occurred. Please tell me how to redirect to CheckView(generic.DetailView) without url include pk.

Alessio Ferri
2#
Alessio Ferri Reply to 2017-12-05 08:42:45Z

You can change the structure of your url to not using a slug, for example:

# 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')),

This is the structure of the urls i am using in my project.. then i am able to manipulate the user, or obtain information from it simply by using request.user!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.337899 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO