我在注册表格中有很多东西要输入,但是我在django 2.2版本中使用基本注册表格。 但是我做不到,我是Django的新手,没有完全了解如何在基本注册表单中添加额外的字段。我这样写:
models.py
from django.db import models
from django.shortcuts import reverse
from django.conf import settings
from home.models import Locations, Levels
class User(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
photo = models.ImageField(upload_to='static/img/profile/', blank=True)
levels = models.ManyToManyField(Levels, verbose_name='levls')
name = models.CharField(max_length=120, db_index=True)
first_name = models.CharField(max_length=120, db_index=True)
birdate = models.DateField()
locations = models.ManyToManyField(Locations, verbose_name='locl')
telnum = models.IntegerField(null=True)
levelreg = models.DateTimeField(auto_now_add=True)
levelchange = models.DateTimeField(auto_now=True)
slug = models.SlugField(max_length=120)
aside = models.CharField(max_length=120, unique=True)
bside = models.CharField(max_length=120, unique=True)
friendusers = models.CharField(max_length=120, unique=True)
rebuy = models.IntegerField(null=True)
balls = models.IntegerField(null=True)
total = models.IntegerField(null=True)
date_pub = models.DateTimeField(auto_now_add=True)
def __str__(self):
return 'Profile for user {}'.format(self.user.username)
forms.py
from django.contrib.auth.models import User
from .models import Profile
from django import forms
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'name', 'first_name', 'birdate', 'telnum', 'email', 'aside',
'bside','rebuy', 'balls', 'total')
def clean_password2(self):
cd = self.cleaned_data
if cd['password'] != cd['password2']:
raise forms.ValidationError('Passwords don\'t match.')
return cd['password2']
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('photo',)
views,py
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from .forms import UserRegistrationForm, UserForm
from .models import User
def register(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
profile_form = UserForm(data=request.POST, files=request.FILES)
if user_form.is_valid() and profile_form.is_valid():
new_user = user_form.save(commit=False)
new_user.set_password(user_form.cleaned_data['password'])
new_user.save()
new_user.refresh_from_db()
new_profile = User.objects.create(user=new_user,
photo=request.FILES['photo'])
new_profile.save()
return render(request, 'users/login.html', {
'new_user': new_user,
'new_profile': new_profile
})
else:
user_form = UserRegistrationForm()
new_profile = UserForm()
return render(request, 'users/registr.html', {
'user_form': user_form,
'new_profile': new_profile
})
在用户名和上传图片时有效,但是在插入更多字段时不起作用
答案 0 :(得分:0)
在cmd中使用以下命令在用户模型makemigrations中添加新字段后
$python manage.py makemigrations
$python manage.py migrate