我正在处理表单模型并收到此错误: 全局名称'AdForm'未定义
在我看来,我有:
来自django.template导入RequestContext,loader 来自django.http导入HttpResponse 来自django.shortcuts导入重定向 来自django.contrib.auth.decorators导入login_required 来自django进口表格
@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))
然而它没有拿起我的模特。我认为我的模型是:
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
有谁知道为什么没有拿起表单模型?
感谢菜鸟。
答案 0 :(得分:2)
在视图的顶部,您需要:
from .models import AdForm
此外,表单通常位于forms.py
,而不是模型。