Question On Django Form Models
我正在处理表单模型,并得到以下错误:未定义全局名称"adform"
在我看来,我有:
来自django.template import requestContext,加载器从django.http导入httpresponse从django.shortcuts导入重定向从django.contrib.auth.decorators导入需要登录名从Django导入表单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @login_required def create(request): if request.POST: ad = AdForm(request.POST) if ad.is_valid(): test = 'valid' else: test = 'invalid' else: test = 'none' template = loader.get_template('ads/create.html') context = RequestContext(request, { 'test': test }) return HttpResponse(template.render(context)) |
但它并没有赶上我的模型。在我看来,我的模型是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | from django.db import models from django.forms import ModelForm TYPE_CHOICES = ( 'Image', 'Code', ) SIZE_CHOICES = ( 'Leaderboard', 'Banner', 'Skyscraper', 'Square', ) class Ad(models.Model): title = models.CharField(max_length=40) type = models.CharField(max_length=7) size = models.CharField(max_length=16) clicks = models.IntegerField() media = models.ImageField(upload_to='ads') link = models.URLField(null=True) created = models.DateTimeField(auto_now_add=True) expires = models.DateTimeField(null=True) def __unicode__(self): return self.name class AdForm(ModelForm): class Meta: model = Ad |
号
有人知道它为什么不采用表单模型吗?
谢谢你。
在您的视图顶部,您需要:
1 | from .models import AdForm |
而且,表单通常在