from django.shortcuts import render
from django.contrib.auth.models import User
from django.shortcuts import redirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate, login
from .forms import LoginForm
def Register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
data = form.cleaned_data
user = User.objects.create(username=data['username'],
# password=data['password1']
)
user.set_password(data['password1'])
user.save()
return redirect('/')
form = UserCreationForm()
context = {
'form': form
}
return render(request, 'register.html', context)
def LoginUser(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
data = form.cleaned_data
user = authenticate(
request, username=data['username'], password=data['passsword'])
print(data)
print(user)
form = LoginForm()
context = {
'form': form
}
return render(request, 'login.html', context)
forms.py
from django import forms
class LoginForm(forms.Form):
username = forms.CharField(max_length=100,)
passsword = forms.CharField(widget=forms.PasswordInput)
output
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
{‘username’: ‘abc’, ‘passsword’: ‘abc’}
None
[11/Jun/2021 17:00:47] “POST /login HTTP/1.1” 200 3330
{‘username’: ‘abc’, ‘passsword’: ‘abc’}
None
[11/Jun/2021 17:01:05] “POST /login HTTP/1.1” 200 3330
Top comments (0)