You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Django自定义User模型创建带外键字段的注册表单

Hey there! Let's fix up your registration form for that custom Django User model of yours. I'll break down each part—form, view, template—so you can get it working smoothly.


First, Let's Align on Your Model Structure

Just to make sure we're on the same page, here's what your models likely look like (adjust field names if yours differ):

# your_app/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models

class Title(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

class Status(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

class CustomUser(AbstractUser):
    filenumber = models.CharField(max_length=20, unique=True)
    intakeyear = models.PositiveIntegerField()
    personalemail = models.EmailField(unique=True)
    title = models.ForeignKey(Title, on_delete=models.CASCADE)
    status = models.ForeignKey(Status, on_delete=models.CASCADE)

Step 1: Build the Custom Registration Form

We'll extend Django's built-in UserCreationForm since it already handles password validation and creation logic. We just need to add your custom fields:

# your_app/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import CustomUser, Title, Status

class CustomUserCreationForm(UserCreationForm):
    # Add explicit fields for your custom attributes (with clear labels)
    personalemail = forms.EmailField(label="Personal Email")
    title = forms.ModelChoiceField(queryset=Title.objects.all(), label="Title")
    status = forms.ModelChoiceField(queryset=Status.objects.all(), label="Status")

    class Meta:
        model = CustomUser
        # List all fields you want in your registration form (order determines rendering sequence)
        fields = (
            'first_name', 'last_name',  # Split "姓名" into first/last; use 'username' if you prefer a single field
            'title', 'status',
            'email', 'personalemail',
            'password1', 'password2'
        )

    # Validate uniqueness of personal email
    def clean_personalemail(self):
        personal_email = self.cleaned_data.get('personalemail')
        if CustomUser.objects.filter(personalemail=personal_email).exists():
            raise forms.ValidationError("This personal email is already registered.")
        return personal_email

Note: If you want a single "姓名" field instead of first/last, you can skip first_name/last_name and use username, or add a custom full_name field to your model and adjust the form accordingly.


Step 2: Create the Registration View

You can use either a class-based view (cleaner, less boilerplate) or a function-based view (more flexible for custom logic). Here's both options:

# your_app/views.py
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
from .forms import CustomUserCreationForm

class SignUpView(CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('login')  # Redirect to login page after successful signup
    template_name = 'signup.html'

Option 2: Function-Based View

# your_app/views.py
from django.shortcuts import render, redirect
from .forms import CustomUserCreationForm

def signup(request):
    if request.method == 'POST':
        form = CustomUserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')  # Adjust this to your desired success page
    else:
        form = CustomUserCreationForm()
    return render(request, 'signup.html', {'form': form})

Step 3: Build the Registration Template

Create a signup.html template that renders the form with proper error handling and user feedback:

<!-- templates/signup.html -->
{% extends 'base.html' %} <!-- Use your existing base template if you have one -->

{% block content %}
<div class="container mt-4">
    <h2>Create Your Account</h2>
    <form method="post" class="mt-3">
        {% csrf_token %}
        
        {% for field in form %}
            <div class="mb-3">
                {{ field.label_tag }}
                {{ field }}
                
                {% if field.help_text %}
                    <small class="text-muted d-block mt-1">{{ field.help_text }}</small>
                {% endif %}
                
                {% for error in field.errors %}
                    <p class="text-danger mt-1">{{ error }}</p>
                {% endfor %}
            </div>
        {% endfor %}
        
        <button type="submit" class="btn btn-primary">Sign Up</button>
    </form>
</div>
{% endblock %}

Step 4: Update Your URLs

Add the signup route to your project's urls.py:

# project/urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from your_app.views import SignUpView  # Or import `signup` if using function-based view

urlpatterns = [
    # ... your other existing URLs
    path('signup/', SignUpView.as_view(), name='signup'),
    # If using function-based view: path('signup/', signup, name='signup'),
    path('login/', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
]

Final Checks Before Testing

  1. Confirm AUTH_USER_MODEL configuration: In your settings.py, ensure you have:
    AUTH_USER_MODEL = 'your_app.CustomUser'
    
  2. Run database migrations: Execute these commands in your terminal:
    python manage.py makemigrations
    python manage.py migrate
    
  3. Test the workflow: Visit /signup in your browser, fill out the form, and verify:
    • Password match validation works
    • Duplicate personal emails trigger an error
    • Title/Status dropdowns load correctly
    • The new user is created in your database

内容的提问来源于stack exchange,提问作者somesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:34:20