开发者

Question On Django Form Models

I am working on form models and get this error: global name 'AdForm' is not defined

In my view I have:

from django.template import RequestContext, loader from django.http import HttpResponse from django.shortcuts import redirect from django.contrib.auth.decorators import login_required from django import forms

开发者_开发知识库@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))

However it is not picking up my model. My model in my view is:

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

Does anyone know why it is not picking up the form model?

Thanks from a noob.


At the top of your view, you need:

from .models import AdForm

Also, forms usually go in forms.py, not with the models.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜