如何在模板渲染渲染Django期间解决此问题错误?

时间:2020-07-31 02:49:39

标签: python django

在Django中渲染模板时出错 我正在尝试运行服务器,但始终显示此错误。我该如何解决这个问题。

在我的item_list.html

{% extends "main/base.html" %}

{% block body %}
    <h1>Here is the list of items.</h1>
    
    {% for item in items %}
        {{ item }}
    {% endfor %}

{% endblock %}

在base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>merobooks</title>
</head>
<body>
    
    {% block body %}
    
    {% endblock %}
    
</body>
</html>

在views.py

from django.shortcuts import render
from . models import Item

def item_list(request):
    context = {
        'items': Item.objects.all()
    }
    return render(request, 'main/item_list.html', context)

在urls.py

from django.urls import path
from . import views

app_name = 'main'

urlpatterns = [
    path('', views.item_list, name='item-list')
]

这显示了以下错误

ProgrammingError at /
relation "main_item" does not exist
LINE 1: ...d", "main_item"."title", "main_item"."price" FROM "main_item...
                                                             ^
Request Method: GET
Request URL:    http://localhost:8000/
Django Version: 3.0.8
Exception Type: ProgrammingError
Exception Value:    
relation "main_item" does not exist
LINE 1: ...d", "main_item"."title", "main_item"."price" FROM "main_item...
                                                             ^
Exception Location: C:\Users\keskh\.virtualenvs\merobooks-K3uFXFWX\lib\site-packages\django\db\backends\utils.py in _execute, line 86
Python Executable:  C:\Users\keskh\.virtualenvs\merobooks-K3uFXFWX\Scripts\python.exe
Python Version: 3.8.1
Python Path:    
['C:\\Users\\keskh\\Desktop\\Python\\dev\\djangoDev\\merobooks',
 'C:\\Users\\keskh\\.virtualenvs\\merobooks-K3uFXFWX\\Scripts\\python38.zip',
 'c:\\users\\keskh\\appdata\\local\\programs\\python\\python38-32\\DLLs',
 'c:\\users\\keskh\\appdata\\local\\programs\\python\\python38-32\\lib',
 'c:\\users\\keskh\\appdata\\local\\programs\\python\\python38-32',
 'C:\\Users\\keskh\\.virtualenvs\\merobooks-K3uFXFWX',
 'C:\\Users\\keskh\\.virtualenvs\\merobooks-K3uFXFWX\\lib\\site-packages']
Server time:    Fri, 31 Jul 2020 01:37:34 +0000

relation "main_item" does not exist LINE 1: ...d", "main_item"."title", "main_item"."price" FROM "main_item... ^

我正在尝试运行服务器,但是它不起作用。总是显示以上错误。我该如何解决这个问题。 需要帮助

1 个答案:

答案 0 :(得分:0)

我将保留此评论,但我的声誉显然太低了。

由于该错误,看来main_item表在您的数据库中不存在。也许您没有执行迁移?

尝试运行此:

./manage.py makemigrations
./manage.py migrate