Exp15 Yash
Exp15 Yash
cd user_auth
import re
email_regex = re.compile(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
confirm_password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
def clean_username(self):
username = self.cleaned_data['username']
if not username_regex.match(username):
if User.objects.filter(username=username).exists():
return username
def clean_email(self):
email = self.cleaned_data['email']
if not email_regex.match(email):
if User.objects.filter(email=email).exists():
return email
def clean_password(self):
password = self.cleaned_data['password']
if not password_regex.match(password):
return password
def clean_confirm_password(self):
password = self.cleaned_data['password']
confirm_password = self.cleaned_data['confirm_password']
if password != confirm_password:
return confirm_password
class UserLoginForm(forms.Form):
username = forms.CharField(max_length=150)
password = forms.CharField(widget=forms.PasswordInput)
def register(request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.save()
return redirect('login')
else:
else:
form = UserRegistrationForm()
def user_login(request):
if request.method == 'POST':
form = UserLoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
login(request, user)
else:
else:
form = UserLoginForm()
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls')),
] <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h2>Register</h2>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
</body>
</html>
INSTALLED_APPS = [
# Other apps...
'accounts',
] MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'python
manage.py runserver
admin.site.register(User) Login
[suleimanpatel] |
[suleiman@example.com] |