我已经用 ModelForm 设置了一个 Django 页面(感谢评论),但现在我的save()
方法没有定义。
当您尝试保存表单时,我加载player_add
视图的页面会给出一个。global name 'save' is not defined
有人可以解释为什么这种保存方法会出错,因为它实际上保存到模型并在我回到我的玩家列表时显示正确的数据?
我的models.py
:
from django.db import models
from datetime import date
from django.core import validators
from django.utils.translation import ugettext as _
import re
def validate_email(value):
return True if validators.email_re.match(value) else False
def validate_phone(value):
value = re.sub(' ', '', value) # get rid of any spaces
return True if re.match(r'/^(\d){11}', value) else False # don't think this regex actually works
class Player(models.Model):
name = models.CharField(max_length = 100)
telnum = models.CharField('Telephone', max_length = 15, validators = [validate_phone],
error_messages = { 'invalid': _('Please enter a valid phone number')})
dob = models.DateField('Date of Birth')
email = models.CharField('MyEmail', max_length = 50, validators = [validate_email],
error_messages = { 'invalid': _('Please enter a valid email address.'),
'required': _('You need to enter a valid email address.')})
class Meta:
db_table = "Players"
ordering = ['name']
class Admin:
pass
def __unicode__(self):
return self.name
forms.py
:
from django.forms import ModelForm
from packages.models import Player
class PlayerForm(ModelForm):
class Meta:
model = Player
我对添加到 Player 模型的看法:
def player_add(request):
form = PlayerForm(request.POST or None)
if form.is_valid():
cmodel = form.save()
cmodel = save()
return redirect(players)
return render_to_response('players/player_add.html',
{'player_form': form},
context_instance = RequestContext(request))