Brenden
Brenden

Reputation: 8764

Saving modelform - won't validate

I'm doing something wrong here, but I can't find it.

I'm using a model form:

class ArtistInfo(ModelForm):
    class Meta:
        model = Onesheet
        fields = (
            'name',
            'genre',
            'location',
            'biography',
        )

And trying to save the data entered for an existing record.

def edit_artist_info(request, onesheet):
    onesheet = Onesheet.objects.get(id = onesheet)
    if request.method == 'POST':
        form = ArtistInfo(request.POST, instance=onesheet)
        if form.is_valid():
            test = form.save(commit=False)
            test.save()
            HttpResponseRedirect('/')
    form = ArtistInfo(instance=onesheet, label_suffix='')
    variables = RequestContext(request, {
        'onesheet':onesheet,
        'form': form,
    })
    return render_to_response('edit_artist_info.html', variables)

But it's not saving. It just reloads the page with whatever the user changed, but if you actually refresh the page (grabbing the value from the DB), it's the old value.

Any ideas? If it's because the form isn't actually validating, I dont know why it's not validating.

Upvotes: 0

Views: 137

Answers (1)

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53981

try just

if request.method == 'POST':
    form = ArtistInfo(request.POST, instance=onesheet)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/')

You were missing the return statement in your code, and the extra save() was unnecessary

Upvotes: 1

Related Questions