For login and logout, I use Django's built-in view in Django.contrib.auth.views. Redirecting the user to a previous page is very easy because you can pass a 'Next' parameter for the login / logout view. They leave work properly
When I want to complete the same functionality for registration, there are some problems. It seems that Django does not have an underlying registration view. So I'm using CreateView with the 'Model = user' code following the following:
# views.py class registration (CreateView): template_name = 'accounts / registration Html 'model = user #Overwrite form_valid () After registering this new user to log on, registration df form_valid (self, form): validation = super (registration, self) .form_valid (form) user name = form .cleaned_data ['username'] password = form.cleaned_data ['password2'] user = certified (user Login = user name, password = password) login (auto request, user) return verification
I no longer have any similar 'next' parameters as in the built-in login / logout view How can I return a new user after registration?
I tried the following method:
Step 1: See the registration that will trigger me from the template, through a GET method, a 'Next' parameter Pass on
& lt; A href = "{% url 'accounts: registration'%}? Next = {{request.path}}" & gt; Registration & lt; / A & gt; By the way, the URL related to the registration is: # accounts / urls.py urlpatterns = Pattern ('', # other URL '^) Register / $ ', views.Registration.as_view (), name =' registration '),)
Step 2: get_context_data () and load get_success_url () ), Is trying to create its own 'next' parameter:
# views.py class registration (CreateView): #df get_context_data above other code (auto, ** Kwargs): Reference = Super (Registration, Self) .get_context_data (** kwargs) Reference [Next] = self.request.GET ['next'] Return Dave Def get_success_url (auto): redirect_to = self.request.POST ['next'] Return
Step 3: In my template, catch next value Add a 'hidden' input field to:
# accounts /registration.html & lt; Form method = "post" verb = "{% url 'accounts: r example'%} '& gt; {% Csrf_token%} & lt; p & gt; {{Form.as_p}} & lt; / p & gt; ; & Lt; input type = "submit" value = "regsitarte" />
This method works fine only when the user does not make a mistake during the registration process. If, for example, the user posts an existing user name, or Email address format is invalid, this method is "Next" A report about the error will report an error.
Can anyone give a method to redirect a user after registration? Or can you change your code to complete this functionality? Instead of writing a new scene from scratch, I want to use Django's built-in class-based general view.
If you skip any HTML form then the If you change your field to remove the hidden field and remove the action, then it should work properly: number Or
URL to clear the attribute parameter and all .
& lt; Form method = "post" & gt; & Lt; P & gt; {{Form.as_p}} & lt; / P & gt; & Lt; Input type = "submit" value = "Regsitarte" /> & Lt; / Form & gt;
No comments:
Post a Comment