Django:发布渲染表单并使用ModelForm提交

时间:2012-11-28 22:53:41

标签: django forms modelform

我正在尝试创建一个允许用户向对象添加Notes的表单。这与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

我的views.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>

问题是表单不呈现。我不确定视图是否到达模板,以便它甚至知道要渲染的形式。

0 个答案:

没有答案