Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

Can't render views of html in django

$
0
0

My models

from django.contrib.auth.models import AbstractUserfrom django.db import modelsclass 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    )

My views.py

from django.shortcuts import render, redirect from .forms import CustomUserCreationForm  # Assuming you have a CustomUserCreationFormdef 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})

My 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>

My forms.py:

from django import formsfrom django.contrib.auth.forms import UserCreationFormfrom .models import CustomUserclass CustomUserCreationForm(UserCreationForm):    class Meta(UserCreationForm.Meta):        model = CustomUser        fields = ('username', 'email', 'user_type')

Output:output


Viewing all articles
Browse latest Browse all 13891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>