django - 如何修复空白页django 2
问题描述
请帮忙。我正在关注以下 django 速成课程: https ://ceiphr.com/blog/a-crash-course-in-django 但在标题为“带样式的模板”的最后一节之后出现空白页。我正在使用 django 2.2.0。
命令树返回的内容。
.
├── blog
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── django_cc
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── manage.py
├── Pipfile
├── Pipfile.lock
└── templates
└── index.html
我的 settings.py 文件如下所示:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
这是 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Posts | Django Crash Course</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css"
crossorigin="anonymous"/>
</head>
<body>
<div class="container">
{% for post in posts %}
<div class="card">
<div class="card-image">
<figure class="image">
<img src="{{ post.image.url }}" alt="Placeholder image"
style="max-width: 250px; max-height: 250px;">
</figure>
</div>
<div class="card-content">
<div class="content">
<b>{{ post.title }}</b> | {{ post.description }}
<br>
<time datetime="{{ post.date }}">{{ post.date }}</time>
</div>
</div>
</div>
{% endfor %}
</div>
</body>
</html>
视图.py
from django.shortcuts import render
from django.views.generic.base import View
from django.views.generic import TemplateView
from blog.models import Post
class PostFeed(TemplateView):
template_name = 'index.html'
def get_context_data(self, *args, **kwargs):
context = super(PostFeed, self).get_context_data(**kwargs)
context["posts"] = Post.objects.all()
return context
模型.py
from django.db import models
# Create your models here.
import datetime
class Post(models.Model):
image = models.FileField(upload_to='images/')
title = models.CharField(default="", max_length=64)
description = models.CharField(default="", max_length=512)
date = models.DateField(default=datetime.date.today)
class Meta:
ordering = ['-date']
def __str__(self):
return self.title
解决方案
您需要返回上下文get_context_data
def get_context_data(self, *args, **kwargs):
context = super(PostFeed, self).get_context_data(**kwargs)
context["posts"] = Post.objects.all()
return context
推荐阅读
- java - 接口混合继承不会在java中抛出错误?
- r - 使用 for 循环(r)创建变量以计算行间“是”的出现次数
- java - 关于理解 Apache Beam 的 wordCount 示例的问题
- ibm-watson - 在 WKS 上使用负面提及类
- elasticsearch - ElasticSearch 查询,匹配某个术语并在给定日期范围内计数
- sql - 如何将 TIMESTAMP 列中的时间与字符串格式列中的时间 (hH:MM:SS) 进行比较
- python - 为大型数据集组合日期、小时和间隔列的更快方法
- python - RegEx如何根据特定位置的字符否定匹配
- html - 如何暂停 CSS 动画
- c# - 使用 HTML 敏捷包进行抓取