3

My problem is similar to how to edit model data using django forms, but I'm not able to solve it. I would like to get an form with prefielled fields and to allow user to edit them. I believe my problem is in views.py file, but unfrotuntely I'm not able to solve it.

models.py

from django.db import models

class Item(models.Model):
    product = models.CharField(max_length=150)
    quantity = models.DecimalField(max_digits=8, decimal_places=3)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    purchase_date = models.DateTimeField()
    warranty = models.DecimalField(max_digits=4, decimal_places=1)
    comment = models.TextField()

forms.py

from django import forms 
from items.models import Item

class EditItemForm(forms.ModelForm):
    class Meta:
        model = Item
        fields = ('product','quantity', 'price', 'purchase_date', 'warranty', 'comment')

urls.py

from django.conf.urls import patterns, include, url 

urlpatterns = patterns('',
    url(r'^show_all/$', 'items.views.items'),
    url(r'^(?P<item_id>\d+)/$', 'items.views.item'),
    url(r'^edit/(?P<item_id>\d+)/$', 'items.views.edit'),
)

edit.html

<form action="/items/edit/" method="post" class="form horizontal well">{% csrf_token %}
    {{ form.as_p }}
    <imput type="submit" class="btn btn-inverse" value="Aktualizuj">
</form>

views.py

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from items.models import Item
from decimal import Decimal
from django.core.context_processors import csrf
from items.forms import EditItemForm
def edit(request):
    if request.method == 'POST':
        form = EditItemForm(request.POST, instance=request.item)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/items/show_all/')

    else:
        form = EditItemForm(instance=item)

    args = {}
    args.update(csrf(request))
    args['form'] = form

    return render_to_response('edit.html', args)

Above code is resulting this message:

TypeError at /items/edit/1/

edit() got an unexpected keyword argument 'item_id'

Can you please help me?

Django 1.6, Python 3.4

2 Answers 2

5

You've imagined an attribute called request.item. There's no such thing. You need to get the item from the database, via the ID passed into the function as alecxe showed.

def edit(request, item_id):
    item = Item.objects.get(pk=item_id)
    if request.method == 'POST':
        form = EditItemForm(request.POST, instance=item)
Sign up to request clarification or add additional context in comments.

2 Comments

There is one remaining problem. Because of the fact that I'm passing "return render_to_response('edit.html', args)" my edit.html file is not able to be populated by {{ item.id }} in <form action="/items/edit/{{ item.id }}/" (...) . How to get this id and put it to html file?
Well, that should be obvious: add the item (or even better, the id) into args. But actually, you don't even need to do that: you're posting to the current URL, so you can just do action=".".
1

edit() view should allow a keyword argument item_id:

def edit(request, item_id=None):
    if request.method == 'POST':
        ...

3 Comments

Yes, this solves this warning. But it still doesnt work. I believe there is problem with "instance". Is there any expected flow of those arguments?
@user3093247 ok, what is happening?
I believe there is problem with "instance". Is there any expected flow of those arguments?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.