I have made the registration part of the login system, but I am unable to make a registered user login. How can I do this?
Here's my code:
Views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.views.generic import View
from .forms import UserForm
# def Success(request):
# return render(request, 'success.html')
class UserFormView(View):
form_class = UserForm
template_name = 'form.html'
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
# cleaned data
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.save()
return redirect('success.html')
return render(request, self.template_name, {'form': form})
Urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from .views import UserFormView
urlpatterns = [
url(r'^$', UserFormView.as_view(), name='registraion'),
url(r'login/', LoginView.as_view(), name='login'),
#url(r'success/', Success, name='sccuess')
]
Forms.html
Registration Form
<br>
<form action="" method="POST">
{% csrf_token %}
{{form.as_p}}
<button type="submit">SUBMIT</button>
</form>
What I basically want is a login page from which I can login in an already-registered user (this login page is different from the page where the user registers). So basically a new view through which a registered user can log in.
I have already made the registration page and the code for it is above. Also after the user is authenticated on the login page he must be redirected to another page (say home.html) and his login information must also we carried to the page where he has been redirected to.