我一直在关注本教程:https://www.youtube.com/watch?v=bRnm8f6Wavk致力于创建一个基本的动态网站。 我设法在 mysql 服务器上创建并测试数据库。但我无法在网页上重现该数据。以下是我必须编辑的文件的代码:
views.py 文件:
from django.shortcuts import render_to_response
from blog.models import posts
def home(request):
entries = posts.objects.all()[:10]
return render_to_response('index.html',{'posts' : entries})
url.py 具有以下内容:
from django.conf.urls import url
from django.contrib import admin
from blog import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
models.py 文件有以下代码
from __future__import unicode_literals
from django.db import models
class posts(models.Model):
author = model.CharField(max_length = 30)
title = models.Charfield(max_length = 100)
bodyText = models.TextField()
timestamp = models.DateTimeField()
settings.py 有以下数据库用于输入:
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Firstblog',
'USER': 'root',
'PASSWORD': '3305',
'HOST': '',
'PORT': '',
}}
我已经编辑了 html 文件:
<title>A BASIC WEBSITE</title>
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<h1> Firstblog </h1>
{% endfor %}
{% for entry in entries %}
{{entry.title}}
<h3> Posted on {{ entry.timestamp }} by {{entry.author}} </h3>
<p>{{entry.body}}</p>
当我运行上面的代码时,我得到的输出只是“FirstBlog”作为标题,但没有数据库发布条目。该视频相当旧,所以我不得不对其进行大量更改。由于我是 django 的新手,我非常不确定如何继续进行下去。我目前有 python 2.7.12(anaconda custom x86-64) 和 django 1.10 谢谢!
最佳答案
模板中的变量称为“posts”
<h1> Firstblog </h1>
{% for entry in posts %}
{{entry.title}}
<h3> Posted on {{ entry.timestamp }} by {{entry.author}} </h3>
<p>{{entry.body}}</p>
{% endfor %}
关于python - 无法在基于 Django 的基本网站上显示 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838380/