您好我最近为管理我的联系表单创建了一个新的应用程序。我有一个名为“手册”的模型和一个模型,其中包含从网站上的联系表单填写的所有信息。但是,当我尝试添加“手册”条目时,我会收到上面显示的错误。这是我的代码,不知道错误出现的原因。
models.py
from django.db import models
class ContactForm(models.Model):
name = models.CharField(max_length="300")
company = models.CharField(max_length="200")
email = models.CharField(max_length="300")
subject = models.TextField()
message = models.TextField()
address = models.TextField()
brochure = models.ManyToManyField("Brochure", null=True)
def __unicode__(self):
self.name
class Meta:
verbose_name = "Contact Enquiry"
verbose_name_plural = "Contact Enquiries"
class Brochure(models.Model):
title = models.CharField(max_length="200")
def __unicode__(self):
self.title
class Meta:
verbose_name = "Brochure"
verbose_name_plural = "Brochures"
views.py
from contact_enquiries import forms
from django.shortcuts import render
from django.http import HttpResponseRedirect
def contact(request):
if request.method == 'POST':
form = forms.ContactEnquiryForm(request.POST)
en_form = forms.BrochureRequestForm(request.POST)
print(request.POST)
if form.is_valid():
#print "Form looks good!"
#e = models.ContactEnquiry(form.cleaned_data)
#e.save()
form.save()
return HttpResponseRedirect('/thanks/')
else:
form = forms.ContactEnquiryForm()
en_form = forms.BrochureRequestForm()
return render(request, 'contact.html', {
'contact_form' : form,
'enquiry_form' : en_form,
})
forms.py 来自django.forms导入ModelForm 来自django进口表格
from contact_enquiries import models
class ContactEnquiryForm(ModelForm):
class Meta:
model = models.ContactForm
fields=('name','email','company', 'subject', 'message')
class BrochureRequestForm(ModelForm):
class Meta:
model = models.ContactForm
fields = ('name', 'email', 'company', 'address', 'brochure',)
widgets = {'brochure': forms.CheckboxSelectMultiple()}
非常感谢任何帮助
STACK TRACE
Traceback:
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/contrib/admin/options.py" in wrapper
372. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/contrib/admin/sites.py" in inner
202. return view(request, *args, **kwargs)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/db/transaction.py" in inner
223. return func(*args, **kwargs)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/contrib/admin/options.py" in add_view
1009. self.log_addition(request, new_object)
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/contrib/admin/options.py" in log_addition
530. action_flag = ADDITION
File "/var/www/website.co.uk/website/lib/python2.6/site-packages/django/contrib/admin/models.py" in log_action
18. e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)
Exception Type: TypeError at /admin/contact_enquiries/brochure/add/
Exception Value: 'NoneType' object is unsubscriptable
答案 0 :(得分:2)
在您的__unicode__
模型方法中ContactForm
需要return
某事
你可以改变
def __unicode__(self):
self.name
要
def __unicode__(self):
return "%s" % self.name
与班级Brochure