Невозможно отобразить представления html в django
Мои модели
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
USER_TYPE_CHOICES = (
('user_type1', 'User Type 1'),
('user_type2', 'User Type 2'),
)
user_type = models.CharField(max_length=20, choices=USER_TYPE_CHOICES)
groups = models.ManyToManyField(
'auth.Group',
related_name='customusers', # Override the default related name
)
user_permissions = models.ManyToManyField(
'auth.Permission',
related_name='customusers_permissions', # Specify a related name for custom user permissions
)
Мой views.py
from django.shortcuts import render, redirect
from .forms import CustomUserCreationForm # Assuming you have a CustomUserCreationForm
def register(request):
if request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
form.save()
# Redirect to a success page or another page
return redirect('success') # Replace 'success' with the URL name of your success page
else:
form = CustomUserCreationForm()
return render(request, 'register.html', {'form': form})
Мой html trample:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Registration</title>
</head>
<body>
<h1>Register</h1>
{% if form.errors %}
<p style="color: red;">There are errors in your form.</p>
<ul>
{% for field, errors in form.errors.items %}
{% for error in errors %}
<li>{{ field }}: {{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
{% endif %}
<form method="post">
{% csrf_token %} <label for="username">Username:</label>
{{ form.username }}<br>
<label for="email">Email:</label>
{{ form.email }}<br>
<label for="password">Password:</label>
{{ form.password }}<br>
<label for="user_type">User Type:</label>
<select name="user_type" id="user_type">
{% for choice in form.user_type.field.choices %}
<option value="{{ choice.0 }}">{{ choice.1 }}</option> {% endfor %}
</select><br>
<button type="submit">Register</button>
</form>
</body>
</html>
Мой forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = ('username', 'email', 'user_type')
Вывод:вывод