Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 569
Kubas Avatar asked Nov 28 '25 20:11

Kubas


2 Answers

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
like image 94
Benjamin Wohlwend Avatar answered Nov 30 '25 09:11

Benjamin Wohlwend


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

like image 27
nmichaels Avatar answered Nov 30 '25 08:11

nmichaels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!