Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing the Form in Django creates new instance

I am editing the form , it loads data correctly buy when i hit save it creates new entry in database.

Here is the view functions

def create_account(request):


    if request.method == 'POST': # If the form has been submitted...
        form = AccountForm(request.POST, request.FILES) # 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() # An unbound form

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

--

def edit_account(request, acc_id):

    f = Account.objects.get(pk=acc_id)
    if request.method == 'POST': # If the form has been submitted...
        form = AccountForm(request.POST, request.FILES) # 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,
    })

Do i really need to have separate function of editing and separate for deleting. Can i do all in one function

template

    <form action="/account/" 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="Send message" /></p>
    </form>
like image 326
Mirage Avatar asked Jun 27 '11 11:06

Mirage


2 Answers

You are missing the instance argument at the POST section.

Instead of this:

form = AccountForm(request.POST, request.FILES) # A form bound to the POST data

You should use this:

form = AccountForm(request.POST, request.FILES, instance=f) # A form bound to the POST data

Once you add that to the add/edit form you will be able to add/edit at the same time.

It will add if instance=None and update if instance is an actual account.

def edit_account(request, acc_id=None):
    if acc_id:
        f = Account.objects.get(pk=acc_id)
    else:
        f = None

    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,
    })
like image 170
Wolph Avatar answered Oct 24 '22 07:10

Wolph


Have you tried something like this?

 # Create a form to edit an existing Object.
     a = Account.objects.get(pk=1)
     f = AccountForm(instance=a)
     f.save()

# Create a form to edit an existing Article, but use
# POST data to populate the form.
    a = Article.objects.get(pk=1)
    f = ArticleForm(request.POST, instance=a)
    f.save()
like image 38
juankysmith Avatar answered Oct 24 '22 05:10

juankysmith