Django 新手,我已经用尽了所有论坛和教程,但我仍然无法将表单数据导入数据库。我有一个简单的模型,它包含一个名称和一个电子邮件字段,我最终将在程序中引用它们。单击提交后我可以加载不同的页面,但我的数据不会发布到数据库。我已经尝试了我能想到的一切,所以我的代码可能在这一点上被劫持了,但在当前的迭代中,这就是我所拥有的:
#models.py
from django.db import models
from django.forms import ModelForm
class Patron(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField(max_length=75)
def _unicode_(self):
return self.name
class PatronForm(ModelForm):
class Meta:
model = Patron
#view.py
from django.shortcuts import render_to_response, get_object_or_404
from patrons.models import Patron
from django.template import RequestContext
from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.forms import ModelForm
def index(request):
if request.method == 'POST':
post = request.POST
name = post['name']
email = post['email']
f = PatronForm(request.Post)
new_patron = f.save()
return render_to_response('patrons/index.html',
context_instance=RequestContext(request))
#html
<body>
<h1>/Picture Taker/</h1>
<form aciton="." name="patron" method="post" >
{% csrf_token %}
<label>
<div>name</div> <input type="text" name="name" id="name" value="{{name}}">
</label>
<label>
<div>email</div> <input type="text" name="email" id="email" value="{{email}}">
</label>
<div class="error">{{error}}</div>
<input type="submit" value="Submit">
</form>
</body>
任何帮助将不胜感激