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

  • Home
  • SEARCH
  • 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 6075191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:28:01+00:00 2026-05-23T10:28:01+00:00

I have 5 Model forms like below class AccountForm(ModelForm): class Meta: model = Account

  • 0

I have 5 Model forms like below

    class AccountForm(ModelForm):
          class Meta:
             model = Account

    class TransactionForm(ModelForm):
          class Meta:
             model = Transaction
.
.
.
.

Now for first form i have this view

def create_account(request, acc_id=None):
    if acc_id:
        f = Account.objects.get(pk=acc_id)
        act1 = 'update/' + acc_id
    else:
        f = None
        act1 = 'create'

    if request.method == 'POST': # If the form has been submitted...
        form = AccountForm(request.POST, request.FILES, instance=f) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            form.save()
            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = AccountForm(instance=f) # An unbound form

    return render_to_response('account_form.html', {
        'form': form,
        'action':act1,
        'type':'account',
    })

Now this view does the editing and creating of new AccountForm.

But i have to do same thing for other five forms. Now i have to copy the same code 5 times with minor chnages. I need to perform the same operation only Form name will be different.

Is there any way that i can use one function for all ModelForms.

The template i use is this

<form action="/{{type}}/{{ action }}/" method="post" enctype="multipart/form-data" >
    {% csrf_token %}
    {% for field in form %}
        <div class="fieldWrapper">
            {{ field.errors }}
            {{ field.label_tag }}: {{ field }}
        </div>
    {% endfor %}
    <p><input type="submit" value="Submit" /></p>
    </form>

so basically template is also same

My URL.py also has to copy same lines like below

   (r'^account/create/$', create_account),
    (r'^account/update/(\d)/$', create_account),
    (r'^txn/create/$', create_txn),
    (r'^txn/update/(\d)/$', create_txn),

Is there any posibility of reducing the code

  • 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-23T10:28:01+00:00Added an answer on May 23, 2026 at 10:28 am

    Store the forms in a dictionary, keyed by the model name.

    FORMS = {
        'account': AccountForm,
        'transaction': TransactionForm,
        ...
    }
    
    def create_object(request, object_class, object_id=None):
        form_class = FORMS[object_class]
        model = form_class._meta.model
        if object_id:
            object = model.object.get(pk=object_id)
    

    … and so on.

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

Sidebar

Related Questions

I have 3 model, User, Post, Comment with definition like below class Post <
I have a form that edits an instance of my model. I would like
Right now, I have more than 25 vertices that form a model. I want
I have a Review Model like the one defined below (I removed a bunch
I have a form view model class that populates Select Lists for use by
I have a simple modal form which I'd like to close when Esc is
I have a model form that contains a DecimalField() with max_digits set to 5.
I have a model that contains a foreign key value, then in the form
I have a jQuery Model windows which contains a form. On creating of the
I have a rails model that validates uniqueness of 2 form values. If these

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.