【发布时间】:2011-06-30 12:48:41
【问题描述】:
我想为模型中的单个字段创建一个编辑表单,其中 textarea 预填充了该字段的当前值。但是,确切的字段名称不是硬连线的,我希望它由 url 指定。
我的模型称为主题。两个示例字段是备注和目标。我可以硬连线字段值,如下所示:
urls.py
(r'^/(?P<topicshortname>\d+)/(?P<whichcolumn>[^/]+)/edit/$', 'mysyte.myapp.views.edit_topic_text'),
views.py
def edit_topic_text(topicshortname, whichcolumn):
thetopic = Topic.objects.get(topic__shortname__iexact = topicshortname)
content = Topic.objects.get(topic__shortname__iexact = topicshortname).objective
return render_to_response('topic_text_edit.html', locals())
topic_text_edit.html
<form method="post" action="../save/">
<textarea name="content" rows="20" cols="60">{{ content }}</textarea>
<br>
<input type="submit" value="Save"/>
</form>
我也可以使用{{ thetopic.objective }} 在模板中进行硬连线,但如果我访问http://mysite.com/topic/Notes/edit/,这两者都会在表单中预先填充目标值,而不是注释值。
我可以使用 'whichcolumn' url 参数来指定对象中要更新的字段吗?
【问题讨论】:
标签: django django-views field