我是django的新手。
我写了一个名为博客的简单django应用程序,每件事情都是正确的。但是当我跑步时,没有外出。
这是我的代码。
model.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
view.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)
的index.html
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
</head>
<body>
<h1>Hello World </h1>
{{x}}
{% for post in posts %}
{{title}}
{% endfor %}
</body>
</html>
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name = 'index')
]
答案 0 :(得分:0)
如果您想查看博客文章的标题:
替换
<!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>