kjarsenal
kjarsenal

Reputation:

save method in a view

I have a very simple model:

class Artist(models.Model):
 name = models.CharField(max_length=64, unique=False)
 band = models.CharField(max_length=64, unique=False)
 instrument = models.CharField(max_length=64, unique=False)

 def __unicode__ (self):
  return self.name

that I'm using as a model form:

from django.forms import ModelForm 
from artistmod.artistcat.models import *

class ArtistForm(ModelForm):
 class Meta:
  model = Artist

but I can't seem to construct a view that will save the form data to the database. Currently I'm using:

def create_page(request):
    if request.method == 'POST':
            form = ArtistForm(request.POST) 
            if form.is_valid(): 
                    form.save()
                    return render_to_response('display.html')
    else:
            form = ArtistForm()
    return render_to_response('create.html', {
        'form': form,
})

can anyone help the newbie?

Upvotes: 1

Views: 1217

Answers (1)

kjarsenal
kjarsenal

Reputation:

Apparently the problem resided in my template. I was using

    <form action="display/" method="POST">

as opposed to

    <form action="." method="POST">

also changed my HttpRequest object from render_to_response to HttpResponseRedirect

true newbie errors but at least it works now

Upvotes: 1

Related Questions