简体   繁体   English

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

[英]How can i solve this issue error during template rendering rendering django?

Error during template rendering in django I'm trying to run the server but it always shows this error. django 中的模板渲染期间出错我正在尝试运行服务器,但它总是显示此错误。 How can i solve this issue.我该如何解决这个问题。

In my item_list.html在我的 item_list.html

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

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

{% endblock %}

In base.html在基地.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>

in views.py在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)

In urls.py在 urls.py

from django.urls import path
from . import views

app_name = 'main'

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

This shows the following error这显示以下错误

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... ^

I'm trying to run the server but it doesn't work.我正在尝试运行服务器,但它不起作用。 Always shows above error.总是显示上述错误。 How can i solve this issue.我该如何解决这个问题。 help needed需要帮助

Had the same issue and spent a lot of time checking the files for typos and other issues, to perform the migrations is pretty obvious still easily forgotten.有同样的问题并花费大量时间检查文件是否存在拼写错误和其他问题,执行迁移非常明显,但仍然很容易被遗忘。

manage.py makemigrations
manage.py migrate 
manage.py runserver

fixed the issue for me, well done!为我解决了这个问题,干得好!

I would leave this as a comment but my reputation apparently is too low.我会将此作为评论,但我的声誉显然太低了。

From the error it looks like the main_item table does not exist in your database.从错误看来,您的数据库中不存在main_item表。 Maybe you haven't performed your migrations?也许您还没有执行迁移?

Try running this:尝试运行这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM