Kubas
Kubas

Reputation: 1028

Django ModelForm - name 'Article' is not defined

I have something like this

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()})

And this is a result - name 'Article' is not defined

this same f.ex. for model = Book and others from ModelForm

Why ?

Upvotes: 3

Views: 4357

Answers (3)

Spacedman
Spacedman

Reputation: 94237

Usually you'd define Article and Book in your models.py file, so from models import Article, Book need to go in your forms code.

Upvotes: 2

Benjamin Wohlwend
Benjamin Wohlwend

Reputation: 31858

Have you defined an Article model somewhere? Usually it's in models.py in the same folder as your forms.py and views.py, e.g.:

from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=100)
    text = models.TextField()

Of course, you'll have to import the Article model in your forms.py:

from models import Article

Upvotes: 7

nmichaels
nmichaels

Reputation: 50991

You never imported any classes named Article or Book. They have to be defined in the namespace before they're used.

Upvotes: 2

Related Questions