关于 Django 表单模型的问题

发布于 2024-12-02 07:15:59 字数 1448 浏览 0 评论 0原文

我正在研究表单模型并收到此错误: 全局名称“AdForm”未定义

在我看来,我有:

from django.template import RequestContext, loader 从 django.http 导入 HttpResponse 从 django.shortcuts 导入重定向 从 django.contrib.auth.decorators 导入 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))

但是它没有选择我的模型。我认为我的模型是:

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

有谁知道为什么它没有选择表单模型?

来自菜鸟的感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喵星人汪星人 2024-12-09 07:15:59

在视图的顶部,您需要:

from .models import AdForm

此外,表单通常位于 forms.py 中,而不是与模型一起。

At the top of your view, you need:

from .models import AdForm

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文