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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:34:12+00:00 2026-05-17T01:34:12+00:00

I am trying to create a simple CRUD with ModelForm. It works fine except

  • 0

I am trying to create a simple CRUD with ModelForm. It works fine except that every time I edit, saving creates a new instance of the data. So i edit and get an extra row in DB instead of an updated one. I am at a loss as to how it knows to save an existing charity as it does not store the PK (id) as a hidden field in the form. That is how I always did it before trying to use the ‘fabulous’ ModelForm!

It’s driving me nuts, I have read everything and as far as I can tell I am doing everything right.

Here is my code..

Model:

from django.db import models
from django.conf import settings

COUNTRY_CHOICES = settings.COUNTRIES

class Charities(models.Model):
    charity_name            = models.CharField(max_length=100)
    country                 = models.CharField(max_length=4, choices=COUNTRY_CHOICES)
    registration_number     = models.CharField(max_length=100)
    address1                = models.CharField(max_length=100)
    address2                = models.CharField(max_length=100)
    city                    = models.CharField(max_length=30)
    zip                     = models.CharField(max_length=10)
    phone                   = models.CharField(max_length=20)
    email                   = models.EmailField()
    charity_logo_image      = models.CharField(max_length=100)
    charity_banner_image    = models.CharField(max_length=100)
    charity_accepted        = models.IntegerField()

    def __str__(self):
       return self.charity_name

    def __unicode__(self):
        self.charity_name

View:

def list(request):
    charities = Charities.objects.all()
    return render_to_response('charities_charity_list.html', {'charities': charities})

def add(request):
    return add_or_edit(request)

def edit(request, charity_id):
    return add_or_edit(request, charity_id)

def add_or_edit(request, charity_id=None):
    print "ID = " + str(charity_id)  
    form = CharityForm(request.POST or None,
                   instance=charity_id and Charities.objects.get(pk=charity_id))

    # Save new/edited student
    if request.method == 'POST' and form.is_valid():
        print form
        form.save()
        return HttpResponseRedirect('/charities/list/')

    return render_to_response('charities_charity_edit.html', {'form': form})

Form:

class CharityForm(ModelForm):
    class Meta:
        model = Charities

Template:

{% extends "base.html" %}

{% block title %}Charities Add{% endblock %}
{% block content %}

<form method="post" action="/charities/add/" id="save"><table cellpadding="0">{{ form.as_table}}</table><input type="submit" value="Save"></form>
{% endblock %}
  • 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-17T01:34:12+00:00Added an answer on May 17, 2026 at 1:34 am

    It doesn`t work because your template is always POSTing to the view that adds a new Charity. When you manually type a URL like /charities/edit/5, it creates the ModelForm with the right initial data, but then POSTs to /charities/add, thus creating a new instance. You need to POST to /charities/edit/5, for example. Take a look at the url template tag.

    I suggest you use 2 templates, one for adding, another for editing. I know it may not be very DRY, but I believe it’s clearer this way.

    Add template:

    {% extends "base.html" %}
    
    {% block title %}Charities Add{% endblock %}
    {% block content %}
    
    <form method="post" action="{% url charities_app.views.add %}"><table cellpadding="0">{{ form.as_table}}</table><input type="submit" value="Save"></form>
    {% endblock %}
    

    Edit template:

    {% extends "base.html" %}
    
    {% block title %}Edit Charity{% endblock %}
    {% block content %}
    
    <form method="post" action="{% url charities_app.views.edit charity.id %}"><table cellpadding="0">{{ form.as_table}}</table><input type="submit" value="Save"></form>
    {% endblock %}
    

    You may also want to check the create_object and update_object generic views, they are very useful in simple cases like yours.

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

Sidebar

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.