O
O
o_may2018-12-22 13:40:51
Django
o_may, 2018-12-22 13:40:51

What is the problem in django views?

Tell me, what's wrong? When I go to 127.0.0.1:8000/archive the following error pops up. What could be the problem?

NameError at /archive/
name 'Article' is not defined
Request Method: GET
Request URL: 127.0.0.1:8000/archive
Django Version: 2.1.4
Exception Type: NameError
Exception Value:
name 'Article' is not defined
Exception Location: / home/alex/djangogirls/myvenv/blog/articles/views.py in archive, line 8
Python Executable: /home/alex/djangogirls/myvenv/bin/python
Python Version: 3.5.2
Python Path:
['/home/alex /djangogirls/myvenv/blog',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
' /usr/lib/python3.5/lib-dynload',
'/home/alex/djangogirls/myvenv/lib/python3.5/site-packages']

Urls.py
from django.contrib import admin
from django.urls import path
import articles
from articles import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('archive/', views.archive, name='archive')
]

Views.py
from articles import models
from django.shortcuts import render

def archive(request):
    return render(request, 'archive.html', {"posts": Article.objects.all()})

And Models.py
from django.db import models
from django.contrib.auth.models import User

class Article(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    text = models.TextField()
    created_date = models.DateField(auto_now_add=True)
    
    def __unicode__(self):
        return "%s: %s" % (self.author.username, self.title)
    
    def get_excerpt(self):
        return self.text[:140] + "..." if len(self.text) > 140 else self.text

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-12-22
@o_may

You have not importedArticle

def archive(request):
    return render(request, 'archive.html', {"posts": models.Article.objects.all()})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question