Django 通过关键字参数动态访问字段的值以传递给模板
我想为模型中的单个字段创建一个编辑表单,其中文本区域预先填充该字段的当前值。但是,确切的字段名称不是硬连线的,我希望它由 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 参数来指定要更新对象中的哪个字段吗?
I want to create an edit form for a single field in my model, where the textarea is prefilled with the current value for that field. However, the exact field name is not hardwired, and I want it to be specified by the url.
My model is called Topic. Two example fields are Notes and Objectives. I can hardwire the field value as in the following:
urls.py
(r'^/(?P<topicshortname>\d+)/(?P<whichcolumn>[^/]+)/edit/
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>
I can also do the hardwiring in the template by using {{ thetopic.objective }}
, but if I visited http://mysite.com/topic/Notes/edit/ both of these would prepopulate the form with the objective value, not the notes value.
Can I use the 'whichcolumn' url argument to specify which field to update in the object?
, 'mysyte.myapp.views.edit_topic_text'),
views.py
topic_text_edit.html
I can also do the hardwiring in the template by using {{ thetopic.objective }}
, but if I visited http://mysite.com/topic/Notes/edit/ both of these would prepopulate the form with the objective value, not the notes value.
Can I use the 'whichcolumn' url argument to specify which field to update in the object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 getattr 按名称获取属性的值。对于您的示例:
但是,您还应该意识到这一点的安全影响。用户将能够通过更改 URL 来编辑他们喜欢的模型上的任何字段。
您应该在对该数据执行任何其他操作之前检查 whichcolumn 的值,或者使用更具体的正则表达式限制 urlconf 中的可能性,例如:
您还提到了字段“Notes”和“Objectives”,但正在访问字段“objective” ',因此您可能需要将 whichcolumn 的值映射到您感兴趣的字段名称,例如:
您应该注意的另一件事是,您通过调用 Topic.objects.get(...) 访问数据库两次两次。您应该重用 thetopic 的值。
, 'mysyte.myapp.views.edit_topic_text', {'whichcolumn': 'note'}),您应该注意的另一件事是,您通过调用 Topic.objects.get(...) 访问数据库两次两次。您应该重用 thetopic 的值。
, 'mysyte.myapp.views.edit_topic_text'),您还提到了字段“Notes”和“Objectives”,但正在访问字段“objective” ',因此您可能需要将 whichcolumn 的值映射到您感兴趣的字段名称,例如:
您应该注意的另一件事是,您通过调用 Topic.objects.get(...) 访问数据库两次两次。您应该重用 thetopic 的值。
You can use getattr to get the value of an attribute by name. For your example:
However, you should be also aware of the security implications of this. Users will be able to edit any field on the model they like by changing the url.
You should either check the value of whichcolumn before doing anything else with that data, or limit the possibilities in the urlconf with a more specific regular expression, eg:
You also mentioned fields 'Notes' and 'Objectives' but are accessing the field 'objective', so you may need to map the values of whichcolumn to the field name you are interested in, eg:
Another thing you should be aware of is that you where accessing the database twice by calling Topic.objects.get(...) twice. You should reuse the value of thetopic.
, 'mysyte.myapp.views.edit_topic_text', {'whichcolumn': 'note'}),Another thing you should be aware of is that you where accessing the database twice by calling Topic.objects.get(...) twice. You should reuse the value of thetopic.
, 'mysyte.myapp.views.edit_topic_text'),You also mentioned fields 'Notes' and 'Objectives' but are accessing the field 'objective', so you may need to map the values of whichcolumn to the field name you are interested in, eg:
Another thing you should be aware of is that you where accessing the database twice by calling Topic.objects.get(...) twice. You should reuse the value of thetopic.
您应该将注释和目标这两个概念分开在两个不同的类中,然后在您的主题主类中使用它们作为参考,
这样您就可以更轻松地检索对象类型并填充正确的类型
you should separate the two concepts of Notes and Objectives in two different classes, then use them in your Topic main class as reference
it would be easier for you to retrieve your object type and populate the correct one