save method in a view

江枫思渺然 提交于 2019-12-24 12:27:19

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/1489041/save-method-in-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!