Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6101477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:30:15+00:00 2026-05-23T13:30:15+00:00

I want to create an edit form for a single field in my model,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:30:16+00:00Added an answer on May 23, 2026 at 1:30 pm

    You can use getattr to get the value of an attribute by name. For your example:

    def edit_topic_text(topicshortname, whichcolumn):
        thetopic = Topic.objects.get(topic__shortname__iexact = topicshortname)
        content =  getattr(thetopic, whichcolumn)
        return render_to_response('topic_text_edit.html', locals())
    

    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:

    (r'^/(?P<topicshortname>\d+)/(?P<whichcolumn>(Notes|Objectives))/edit/$', '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:

    (r'^/(?P<topicshortname>\d+)/Objectives/edit/$', 'mysyte.myapp.views.edit_topic_text', {'whichcolumn': 'objective'}),
    (r'^/(?P<topicshortname>\d+)/Notes/edit/$', '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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a form that has a single text field (for company): class Cas_Form_Company
Relativly new to EXTJS, I have an edit form that I want to show
I want to create script to edit photos in galery. I have there textbox
I want to create simple app able to edit images. Main view of app
EDIT: Simple version of the question: I want to create server variables in the
I create an audio buffer and want to edit this before I play this
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
So I have model with an association and I'm using a single form with
Am trying to simplify the create / edit view for a multi-model controller. User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.