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 9308863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:43:47+00:00 2026-06-19T00:43:47+00:00

I’m trying out a single view function which will display an empty form and

  • 0

I’m trying out a single view function which will display an empty form and also will display a previously filled form for further editing. This is the view I have at present:

def manage_contacts(request):
    ContactsFormSet = modelformset_factory(Contact)
    if request.method == 'POST':
        formset = ContactsFormSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            return HttpResponseRedirect('/people/')
    else:
        formset = ContactsFormSet(queryset=Contact.objects.none())
    return render_to_response("contact.html", {
        "formset": formset,
    })

def edit_form(request, item_id):
    ContactsFormSet = modelformset_factory(Contact)
    if request.method == 'POST':
        instance = get_object_or_None(ContactsFormSet, pk=item_id)
        formset = Contact(request.POST, instance=instance)
        return render_to_response("contact.html", {
            "formset": formset,
    })

Presently I have two functions: first one shows a new form and the second one for editing existing data. The second function gives an error. I couldn’t get modelformset_factory display data from existing database column. However, the first function works. The snag I face is that I can’t find a way to rewrite both as a single view function. This is what I have in models: https://stackoverflow.com/a/14724113/498309

#urls.py
url(r'^edit/(?P<item_id>\d+)/$', 'app.views.edit_form'),

Update 1

def contact_view(request, item_id=None):
    context_data = {}
    if item_id:
        contact = get_object_or_404(Contact, pk=item_id)
    if request.method == 'POST':
        forms_are_valid = True
        new_form = ContactForm(request.POST, prefix='new')
        if new_form.is_valid():
            new_form.save()
        else:
            forms_are_valid = False
            context_data['new_form'] = new_form
        if item_id:
            existing_form = ContactForm(request.POST, instance=contact,
                prefix='existing')
            if existing_form.is_valid():
                existing_form.save()
            else:
                forms_are_valid = False
                context_data['existing_form'] = existing_form 
        if forms_are_valid: 
            return HttpResponseRedirect('thanks/')
    else: 
        new_form = ContactForm(prefix='new')
        context_data['new_form'] = new_form
        if item_id:
            existing_form = ContactForm(instance=contact, prefix='existing')
            context_data['existing_form'] = existing_form
    return render_to_response('contact.html', context_data)

This view does not display the form. But it does not show any errors and it shows just the html template.

  • 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-06-19T00:43:48+00:00Added an answer on June 19, 2026 at 12:43 am

    I wouldn’t use a modelformset to do what you’re suggesting. Just use two forms in the same view, adding a prefix to differentiate between them –

    from django.http import HttpResponseRedirect
    from django.shortcuts import get_object_or_404, render_to_response
    
    def contact_view(request, contact_id=None):
        context_data = {}
        if contact_id:
            contact = get_object_or_404(pk=contact_id)
        if request.method = 'POST': 
            forms_are_valid = True
            new_form = ContactForm(request.POST, prefix='new')
            if new_form.is_valid():
                new_form.save()
            else:
                forms_are_valid = False
                context_data['new_form'] = new_form  errors
            if contact_id:
                existing_form = ContactForm(request.POST, instance=contact,
                                            prefix='existing')
                if existing_form.is_valid():
                    existing_form.save()
                else:
                    forms_are_valid = False
                    context_data['existing_form'] = existing_form 
            if forms_are_valid: 
                return HttpResponseRedirect('thanks/')
        else: 
            new_form = ContactForm(prefix='new')
            context_data['new_form'] = new_form
            if contact_id:
                existing_form = ContactForm(instance=contact, prefix='existing')
                context_data['existing_form'] = existing_form
        return render_to_response('contact.html', context_data)
    

    The contact_id argument in the definition allows you to use the view for both new contacts and existing contacts. Have two url’s point to the same view, one that includes the contact_id and one that doesn’t –

    url(r'^contact/(?P<contact_id>\d+)/$', 'app.views.contact_view'),
    url(r'^contact/$', 'app.views.contact_view'),
    

    The view checks to see if the item_id is being passed – if is then we know we’re dealing with an existing contact.

    I’m afraid I haven’t actually run this code (and it’s slightly longer than I expected), so it may be buggy, however I’ve used this pattern many times before – it does work.

    UPDATE

    The view passes two forms to the template new_form and existing_form. The simplest way to output these forms in the template would be –

    <form action="/contact/" method="post">
        {{ existing_form.as_p }}
        {{ new_form.as_p }}
        <input type="submit" value="Submit" />
    </form>
    

    have a look a the docs on customizing the form template for more info.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.