首页 > 解决方案 > 如何修复空白页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

标签: django

解决方案


您需要返回上下文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

推荐阅读