我已经使用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
我对添加到播放器模型的观点:
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))