How to place a registration form on the same index page

At the moment I have my registration form and my registration form on two different pages, I can not present them in one view, because, as I did, the views require different return statements. So here is my registration view function:

def register_user(request): if request.method == 'POST': form = MyRegistrationForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/accounts/register_success') args = {} args.update(csrf(request)) args['form'] = MyRegistrationForm() return render_to_response('register.html', args) def register_success(request): return render_to_response('register_success.html') 

and here is my login and authentication:

 def login(request): c = {} c.update(csrf(request)) return render_to_response('login.html', c) def auth_view(request): username = request.POST.get('username', '') password = request.POST.get('password', '') user = auth.authenticate(username=username, password=password) if user is not None: auth.login(request, user) return HttpResponseRedirect('/accounts/loggedin') else: return HttpResponseRedirect('/accounts/invalid') 

Here is the template for the login page:

  <form action="/accounts/auth/" method="post">{% csrf_token %} <label for="username">User name:</label> <input type="text" name="username" value="" id="username"> <label for="password">Password:</label> <input type="password" name="password" value="" id="password"> <input type="submit" value="login" /> </form> 

and here is the registration page template:

 <h2>Register</h2> <form action="/accounts/register/" method="post">{% csrf_token %} {{form}} <input type="submit" value="Register" /> </form> 

Again, I cannot imagine them together, because the views must return different things .. any idea on how to do this?

EDIT: This is my urls.py:

 url(r'^admin/', include(admin.site.urls)), url(r'^$', index), url(r'^accounts/auth/$', auth_view), url(r'^invalid/$', invalid_login), url(r'^accounts/register/$', register_user), url(r'^accounts/register_success/$', register_success), 

so it will only use register_user view, if url is account / register, I want the user to view register_view if this is the homepage (^ $). My index is like this:

 def index(request): c = {} c.update(csrf(request)) return render_to_response('index.html', c) 

it basically just adds the csrf token to my index.html (registration template, as seen above). That's why I want to be able to somehow combine the index and the register_user view, since view_user calls the actual form = MyRegistrationForm (request.POST), which is used in the registration template (the registration template uses {{form}}

+4
source share
2 answers

If you just want to show the login and registration on the same page. Create an html page with two forms. The action of one of them on url '/accounts/register/' , and the other on '/accounts/auth/' .

for example, change your register.html to:

 <html> <head><body> <form action="/accounts/auth/" method="post">{% csrf_token %} <label for="username">User name:</label> <input type="text" name="username" value="" id="username"> <label for="password">Password:</label> <input type="password" name="password" value="" id="password"> <input type="submit" value="login" /> </form> <h2>Register</h2> <form action="/accounts/register/" method="post">{% csrf_token %} {{form}} <input type="submit" value="Register" /> </form> </body> </html> 

When you submit the form, it will go to the corresponding view and redirect based on that view.

EDIT: to pass an instance of MyRegistrationForm you can update the view index as follows:

 def index(request): c = {} c.update(csrf(request)) c.update({'form':MyRegistrationForm()}) return render_to_response('index.html', c) 

use answer.html as above in the HTML code.

+7
source

You can do this with a little Javascript. First create an input field:

 <input type="hidden" name="button-name" id="button-name" value="" /> <input type="submit" name="register-button" id="register-button" onclick="changeValue()" /> function changeValue(){ document.getElementById('button-name').value="register" } 

Now in your views do the following:

 button_name = request.POST['button-name'] if button-name=='register': # Do the registration # Redirect to another view or template else: # Do your login thing # Redirect where you want 
0
source

Source: https://habr.com/ru/post/1493884/


All Articles