在视图中保存方法 [英] 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
m使用作为模型窗体:
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?
推荐答案
显然问题存在于我的模板中。我正在使用
Apparently the problem resided in my template. I was using
<form action="display/" method="POST">
而不是
<form action="." method="POST">
还将我的HttpRequest对象从 render_to_response
更改为 HttpResponseRedirect
also changed my HttpRequest object from render_to_response
to HttpResponseRedirect
真正的新手错误,但至少现在可以工作
true newbie errors but at least it works now
这篇关于在视图中保存方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文