python - Django update model entry using form fails -


i want update model entry using form. problem instead of updating entry creates new entry.

def edit(request, c_id):     instance = get_object_or_404(c, id=int(c_id))      if request.post:         form = cform(request.post, instance=instance)         if form.is_valid():             form.save()              return redirect('/a/b', c_id)     else:         form = cform(instance=instance)          args = {}         args.update(csrf(request))         args['form'] = form         args['c_id'] = c_id          return render_to_response('a/b.html', args) 

html code:

<form action="/a/edit/{{ c_id }}/" method="post">     {% csrf_token %}     {% field in form %}         <div class="fieldwrapper">             {{ field.errors }}             {{ field.label_tag }} {{ field }}             {% if field.help_text %}                 <p class="help">{{ field.help_text|safe }}</p>             {% endif %}         </div>     {% endfor %}     <input type="submit" value="submit"/> </form> 

cform class code

class cform(forms.modelform):     class meta:         model = c         fields = ['name', 'code'] 

you're checking request post method incorrectly. request.post isn't boolean, contains dictionary of post variables , going have csrf token in "truthy". need request.method.

instead of:

if request.post: 

replace with:

if request.method == "post": 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -