Django ModelForm - 名称'Article'未定义

Kub*_*bas 2 python django django-forms

我有类似的东西

from django.forms import ModelForm
from django.shortcuts import render_to_response

class ArticleForm(ModelForm):
    class Meta:
        model = Article

def articl(request):
    tykul = ArticleForm()
    return render_to_response('test.html',{'tykul':tykul.as_ul()})
Run Code Online (Sandbox Code Playgroud)

这是一个结果 - 名称'Article'没有定义

同样的f.ex. for model = Book和其他来自ModelForm

为什么?

Ben*_*end 7

你有没有在Article某个地方定义模型?通常它是在models.py同一文件夹作为您forms.pyviews.py,如:

from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=100)
    text = models.TextField()
Run Code Online (Sandbox Code Playgroud)

当然,您必须在以下位置导入Article模型forms.py:

from models import Article
Run Code Online (Sandbox Code Playgroud)