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.

链接地址: http://www.djcxy.com/p/56534.html

上一篇: 我们可以格式化模型表单在模板中如何在Django中显示模型表单

下一篇: Django表单模型的问题