Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put login and registration form on same index page

As of now, I have my registration form and my log in form on two different pages, I can't seem to put them in one view because the way I did it, 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 log in and authentication view:

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 log in 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 template for the registration page:

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

<input type="submit" value="Register" />

</form>

Again, I can't seem to put them together because the views have to return different things.. any idea on how to do it?

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 the register_user view if the url is accounts/register, I want it to user the register_view view if it is the homepage (^$). My index view is this:

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

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

like image 906
user216485 Avatar asked Jul 28 '13 05:07

user216485


1 Answers

If you just want to show login and registration on the same page. Create a html page with two forms. Action of one of them to url '/accounts/register/' and another one to '/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 submit the form, it will go to corresponding view and redirect based on that view.

EDIT:To pass a MyRegistrationForm instance you can update the index view as follows:

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

use the login.html as above html code in the answer.

like image 116
Ranju R Avatar answered Sep 20 '22 04:09

Ranju R