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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:26:50+00:00 2026-05-26T19:26:50+00:00

I have a ModelForm that I have created a view and template with to

  • 0

I have a ModelForm that I have created a view and template with to add an instance to the database. I am now trying to extend this to be editable when the user clicks a button – but when they do that the form/template page appears and I get the “This Field is required message” but all fields are empty instead of pre-populated with the instance I passed in, but when I edit the values to something different than originally then in my database the correct instance is updated. So it is passing the primary key but none of the values are showing up. If anyone can tell what I am doing wrong I would appreciate it, oh, and I am using this post Django edit form based on add form? as a basis so please don’t just send me there.

Here are my files

ModelForm

class CreditCardForm(forms.ModelForm):
class Meta:
    model = CreditCard
    fields = ('name_on_card','card_number','contact_number_on_card')

View

def edit(request, id=None, template_name='article_edit_template.html'):
if id:
    print "Edit Mode"
    card = get_object_or_404(CreditCard, pk=id)
    if card.card_owner != request.user:
        raise HttpResponseForbidden()
else:
    print "Create Mode"
    card = CreditCard(card_owner=request.user)

if request.POST:
    print "request.POST"
    form = CreditCardForm(request.POST, instance=card)
    if form.is_valid():
        print "is_valid()"
        form.save()

        # If the save was successful, redirect to another page
        # redirect_url = reverse(article_save_success)
        return HttpResponseRedirect('/cards/')
else:
    print "else"
    form = CreditCardForm(instance=card)

return render_to_response(
    'create_credit.html', 
    {'form': form,},
    context_instance=RequestContext(request)
)

Template

{% include "base.html" %}

<form action="" method="post">{% csrf_token %}
<fieldset>
<legend>Required</legend>

<div class="fieldWrapper">
    {{ form.name_on_card.errors }}
    <label for="id_topic">Name as it appears on card:</label>
    {{ form.name_on_card }}
</div>
<div class="fieldWrapper">
    {{ form.card_number.errors }}
    <label for="id_topic">Last 6 digits of card number:</label>
    {{ form.card_number }}
</div>  
</fieldset>

<fieldset>
<legend>Optional</legend>
<!-- This is for Credit Card's Only -->
<div class="fieldWrapper">
    {{ form.contact_number_on_card.errors }}
    <label for="id_topic">Institution contact number: 1-</label>
    {{ form.contact_number_on_card }}
</div>
</fieldset>
<p><input type="submit" value="Save"></p>
</form>

URLS

    url(r'^new/credit/$', views.edit, {}, 'crds_newCredit'),
url(r'^edit/credit/(?P<id>\d+)/$', views.edit, {}, 'crds_editCredit'),
  • 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-26T19:26:51+00:00Added an answer on May 26, 2026 at 7:26 pm

    @xizor use a link instead a button to redirect to the edition form, becasue with the button you are probably sending a post to the view.

    PS: Please forgive my English, edit this answer if you think it is right to make it more useful.

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

Sidebar

Related Questions

I am trying to make a view that allows a user to edit a
In a ModelForm, i have to test user permissions to let them filling the
If you have 2 views, the first uses modelform that takes inputted information from
I have a multiple ModelForm classes that each represent a different Model. I would
I have a registration form that when the user enters in valid registration data
I have a video model that stores 'likes' as a manytomany field with User.
I have a modelform that will either create a new model or edit an
I have a form looking like this: class MarketingActionForm(forms.ModelForm): contact = ManyToManyByLetter(Contact, field_name=first_name) #contact
I have a Django app that allows the user to create variables and name
I have a this model: class Fleet(models.Model): company = models.ForeignKey(Company, editable=False) aircraft = models.ForeignKey(Aircraft)

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.