o_may
@o_may
junior QA

В чем проблема в django views?

Подскажите, в чем ошибка? Когда захожу на 127.0.0.1:8000/archive выплывает следующая ошибка. В чем может быть проблема?

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


И 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
  • Вопрос задан
  • 448 просмотров
Решения вопроса 1
sergey-gornostaev
@sergey-gornostaev Куратор тега Django
Седой и строгий
Вы не импортировали Article

def archive(request):
    return render(request, 'archive.html', {"posts": models.Article.objects.all()})
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы