我正在尝试创建一个允许用户向对象添加注释的表单。它类似于 django 评论包,但我无法让它工作(django 文档中的教程不起作用)。
这是我的模型和 ModelForm:
class Note(models.Model):
text = models.CharField(max_length = 200)
thing_about = generic.GenericForeignKey('content_type','object_id')
content_type = models.ForeignKey(ContentType,
limit_choices_to={"model__in": ("Individual", "ArtsOrganization",'Presenter', "BookingAgent","Festival","ManagementCompany", "OtherOrganization","PresenterCompany", "Venue", "Gig", "Venue")},blank=True, null=True)
object_id = models.IntegerField(db_index=True, blank=True, null=True,)
date_added = models.DateTimeField(('date added'), auto_now_add=True)
date_modified = models.DateTimeField(('date modified'), auto_now=True)
author = models.ForeignKey(User)
def __unicode__(self):
return u'%s' % (self.date_added)
class NoteForm(ModelForm):
class Meta:
model = Note
我的观点.py
def note(request):
if request.method == 'POST': # If the form has been submitted...
form = NoteForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
form.save()
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = NoteForm() # An unbound form
return render(request, 'includes/note.html', {
'form': form,
})
我在 myapp/templates/includes/note.html 下创建了一个模板,这样我就可以为每个对象使用该表单(它们都有不同的详细信息页面),即使用相同的表单在 Band 页面和 Venue 页面上做笔记。
这是模板“includes/notes.html”的内容:
<form action="includes/note/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
问题是表单不呈现。我不确定视图是否到达模板,因此它甚至知道要呈现什么形式。