开发者

Django: save() method for ModelForm is not updating the database

I'm having a problem with saving and updating a value to my database.

View:

def code_prof_edit(request, ampCode):
    user_code = get_object_or_404(CodeUser, pk=ampCode)
    if request.method =='POST':
        form = CodeUserForm(request.POST, instance=user_code)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/codes/' + user_code.ampCode)
    else:
        form = CodeUserForm(instance=user_code)
        return render_to_response('edit_code_user.html', {'user_code':user_code, 'form':form})

Relevant template form tag:

<form action="." method="POST"> {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Save" />
</form>

Using the post method, the form correctly renders in the browser with all of the original values pre-populated, but if I change the values and click submit, none of the changes are made. Everything seems to work correctly in that the user is redirected to the correct page which means the save must have been called开发者_如何转开发, but the info typed isn't being saved. Any and all help would be appreciated.

Edit entire view function:

from django.shortcuts import render_to_response, get_object_or_404
from django.http import *
from django.template import RequestContext
from codeuser.models import CodeUser
from codeuser.forms import CodeUserForm
import pdb

def code_prof_edit(request, ampCode):
    user_code = get_object_or_404(CodeUser, pk=ampCode)
    if request.method =='POST':
        pdb.set_trace()
        form = CodeUserForm(request.POST, instance=user_code)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/codes/' + user_code.ampCode)
        else:
            return render_to_response('home.html')
    else:
        form = CodeUserForm(instance=user_code)
        return render_to_response('edit_code_user.html', {'user_code':user_code, 'form':form})

Edit: Figured it out. In the html form tag, the action attribute was incorrect. "." wasn't working, I needed "./edit" to get the correct result. Also, excluding that attribute all together worked perfectly too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜