您好我刚读完了http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/,并尝试将其应用到我自己的django应用程序中。
我创建了一个视图,它允许我使用单个表单来创建和存储彼此相关的2个对象
我收到了错误信息,但似乎找不到导致错误的原因。
单击添加后,将显示一个空白页面以及下面的错误消息,该消息在终端中显示为红色。
'[28 / May / 2014 02:57:25]“POST / members HTTP / 1.1”405 0'
模型
class MemberRole(models.Model,get_fields):
name = models.CharField(max_length = 20)
def __unicode__(self):
return self.name
class Member(models.Model,get_fields):
first_name = models.CharField(max_length = 20)
role = models.ForeignKey(MemberRole, null = True, blank = True)
表单
class MemberForm(forms.ModelForm):
class Meta:
model = Member
exclude = ('role',)
class MemberRoleForm(forms.ModelForm):
class Meta:
model = MemberRole
查看功能
def add_member(request):
model_url = 'member-list'
if request.method == "POST":
rform = MemberRoleForm(request.POST, instance=MemberRole())
mform = MemberForm(request.POST, instance=Member())
if rform.is_valid() and mform.is_valid():
new_role = rform.save()
new_member = mform.save()
return HttpResponseRedirect('member-list')
else:
rform = MemberRoleForm(instance=MemberRole())
mform = MemberForm(instance=Member())
return render_to_response('create_model.html', {'role_form': rform, 'member_form': mform, 'model_url': model_url,},context_instance=RequestContext(request))
create_model.html的片段
<form action="{% url model_url %}" method="POST">
{% csrf_token %}
{% if model_url == 'member-list' %}
{% for field in member_form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
{% for field in role_form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
{% endif %}
<input id="save_contact" type="submit" value="Add"/>
</form>
答案 0 :(得分:2)
我已经解决了,这是由于网址命名中的一些错误
查看
model_url = 'member-add'
return HttpResponseRedirect('members')
<强> URLS 强>
url(r'^members', ModelListView.as_view(model = Member,page_name = 'Members',edit_link = 'updatemember/'), name='member-list'),
url(r'^addmember', 'inventory.views.add_member', name = 'member-add'),