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}}