urls.py
I am trying to create a new user with the sign-up page, but it just returns a blank form when I fill it out and press sign up. I created a superuser and am able to login with that and it works fine but can’t figure out the problem with the signup page.
views.py
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import authenticate, login
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .models import Profile
from .forms import ProfileForm
def login_view(request):
if request.method == 'POST':
form = AuthenticationForm(request, request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username,password=password)
if user is not None:
login(request, user)
return redirect('dashboard')
else:
messages.error(request, 'Invalid username or password.')
else:
form = AuthenticationForm()
return render(request, 'login.html', {'form': form})
def home_view(request):
return render(request, 'home.html')
def signup_view(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = UserCreationForm()
return render(request, 'signup.html', {'form': form})
from django.shortcuts import render
def dashboard_view(request):
user = request.user
context = {
'user': user,
}
return render(request, 'dashboard.html', context)
@login_required
def profile_view(request):
profile = request.user.profile
if request.method == 'POST':
form = ProfileForm(request.POST, request.FILES, instance=profile)
if form.is_valid():
form.save()
return redirect('profile')
else:
form = ProfileForm(instance=profile)
return render(request, 'profile.html', {'form': form})`
signup.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign Up</title>
<link href="{% static 'styles.css' %}" rel="stylesheet" s>
</head>
<body>
<h1>Sign Up</h1>
<form method="post">
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br>
<button type="submit">Sign Up</button>
</form>
</body>
</html>
4