未定义Django Teamplate错误名称'Post_title'
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未定义Django Teamplate错误名称'Post_title'相关的知识,希望对你有一定的参考价值。
我正在尝试呈现索引html并从数据库中获取帖子标题,但出现错误。我在视图发布数据库中定义,但仍然出现错误
未定义名称“ Post_title”
我的app / views.py
from django.shortcuts import render, get_object_or_404
from django.shortcuts import reverse
from .models import BlogPost,comments
def index(request):
Post_list = BlogPost.objects.all()
template_name = 'front/index.html'
return render(request, template_name,Post_title:"Post_title",)
def post_detail(request):
return render(request, 'front/post_detail.html')
我的应用程序/urls.py
from django.urls import path
from .import views
urlpatterns = [
path('', views.index, name = 'index'),
path('<int:BlogPost_id>/', views.post_detail, name='Post Detail')
]
我的项目/urls.py
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from froala_editor import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('froala_editor/', include('froala_editor.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
我的index.html模板
<div class="col-md-8 mt-3 left">
% for post in Post_list %
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title"> post.Post_title </h2>
</div>
</div>
% endfor %
</div>
答案
return render(request, template_name, 'Post_list':Post_list)
以上是关于未定义Django Teamplate错误名称'Post_title'的主要内容,如果未能解决你的问题,请参考以下文章