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/$', '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>
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?