[英]Using an extended usermodel, how can I create two model instances with two modelforms in the same view?
First off, I want to say that I am new to django though I understand most of it, forms have always been confusing to me. 首先,我想说我是django的新手,尽管我了解其中的大部分内容,但表格一直让我感到困惑。
I have two models in which I need to create an instance from, the standard built in User
model and my own UserProfile
model. 我有两个需要从中创建实例的模型,即内置的User
模型和我自己的UserProfile
模型。
The problem I am facing is that really do not know how to display two forms (one for each model) in the template and on save()
then tell django that the newly created UserProfile
instance's User = model.ForeignKey
belongs to the also newly created User
. 我面临的问题是真的不知道如何在模板中和在save()
上显示两种形式(每个模型一种),然后告诉django新创建的UserProfile
实例的User = model.ForeignKey
属于新创建的User
。
My forms.py is fairly simple: 我的forms.py非常简单:
from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from .models import UserProfile
class UserForm(ModelForm):
class Meta:
model = User
fields = ('username', 'email', 'password')
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ('display_name', 'avatar', 'birthday', 'usertype', 'daw', 'usergenre')
In my views.py I've tried doing this: 在我的views.py中,我尝试这样做:
from .forms import UserForm
from .forms import UserProfileForm
# Create your views here.
def RegisterView(request):
if request.method == 'POST':
form = UserForm(request.POST)
form2 = UserProfileForm(request.POST)
if form.is_valid():
if form2.is_valid():
return HttpResponseRedirect('/login/')
return render(request, 'register.html', {'form': form, 'form2': form})
But when I try to access /register
I get this error: http://dpaste.com/19NH2A6 但是,当我尝试访问/register
此错误: http : //dpaste.com/19NH2A6
You should use save() method. 您应该使用save()方法。 Next time you have a problem, check the official Django documentation. 下次遇到问题时,请查看官方Django文档。 ModelForms - save() method ModelForms-save()方法
from .forms import UserForm
from .forms import UserProfileForm
from django.http import HttpResponseRedirect
def RegisterView(request):
if request.method == 'POST':
form = UserForm(request.POST, prefix='uf')
form2 = UserProfileForm(request.POST, prefix='upf')
if form.is_valid():
if form2.is_valid():
form.save()
form2.save()
return HttpResponseRedirect('/login/')
elif request.method == 'GET':
form = UserForm(prefix='uf')
form2 = UserProfileForm(prefix='upf')
return render(request, 'register.html', {'form': form, 'form2': form2})
When you hit /register
page from browser, your view gets GET
request, you have created form only for POST
request 当您从浏览器点击/register
页面时,您的视图将获得GET
请求,而您仅为POST
请求创建了表单
from .forms import UserForm
from .forms import UserProfileForm
# Create your views here.
def RegisterView(request):
if request.method == 'POST':
form = UserForm(request.POST, prefix='uf')
form2 = UserProfileForm(request.POST, prefix='upf')
if form.is_valid():
if form2.is_valid():
return HttpResponseRedirect('/login/')
elif request.method == 'GET':
form = UserForm(prefix='uf')
form2 = UserProfileForm(prefix='upf')
return render(request, 'register.html', {'form': form, 'form2': form2})
You should read the traceback yourself so that you can find the error easily. 您应该自己阅读回溯,以便可以轻松找到错误。 It says; 它说;
Exception Type: NameError at /register/ Exception Value: name 'HttpResponseRedirect' is not defined 异常类型:/ register /处的NameError异常值:未定义名称'HttpResponseRedirect'
You've used HttpResponseRedirect but you didn't import it, therefore it is not defined. 您已经使用了HttpResponseRedirect,但没有导入它,因此未定义它。
Add this to top of your code. 将此添加到您的代码的顶部。
from django.http import HttpResponseRedirect
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.