Django项目无输出

2024-04-20 05:49:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚到django。你知道吗

我写了一个简单的django应用程序叫blog,每件事都是正确的。但当我跑的时候就没有出路了。你知道吗

这是我的密码。你知道吗

型号.py

from django.db import models

class Blog_post(models.Model):
    title = models.CharField(max_length=250)
    body = models.TextField(max_length=2000)
    date = models.DateTimeField()
    auther = models.CharField(max_length=250)


    def __str__(self):
        return self.title

视图.py

from django.shortcuts import render
from .models import Blog_post

def index(request):
    posts = Blog_post.objects.all()
    context = {'posts': posts}
    return render (request, 'blog/index.html', context)

索引.html

<!DOCTYPE html>
<html>
<head>
    <title>My Blog</title>
</head>
<body>
<h1>Hello World </h1>
     {{x}}

    {% for post in posts %}
        {{title}}

    {% endfor %}
</body>
</html>

网址.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name = 'index')
]

Tags: djangofrompyimportindextitlemodelshtml
1条回答
网友
1楼 · 发布于 2024-04-20 05:49:14

如果您想查看博客文章的标题:

替换

<!DOCTYPE html>
<html>
<head>
    <title>My Blog</title>
</head>
<body>
<h1>Hello World </h1>
     {{x}}

    {% for post in posts %}
        {{title}}

    {% endfor %}
</body>
</html>

使用:

<!DOCTYPE html>
<html>
<head>
    <title>My Blog</title>
</head>
<body>
<h1>Hello World </h1>
     {{x}}

    {% for post in posts %}
        {{ post.title }}

    {% endfor %}
</body>
</html>

相关问题 更多 >