python - 无法在基于 Django 的基本网站上显示 mysql 数据库

标签 python mysql django

我一直在关注本教程: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/

相关文章:

python - 如何在django中以最 "secure"的方式暴露用户密码?

Python 2.7 - 评估列表中的元素以在 for 循环中使用

python - Pandas 将行与条件进行比较

python - 将值向后插入 Pandas Dataframe(从高索引到低索引)

mysql - 为什么MySQL使用子查询的LIMIT条件搜索比简单搜索更快?

php - 安全高效的文件 uploader

python - 文件未找到错误: [WinError 3] The system cannot find the path specified: ''

mysql - 自动化 Putty 来完成日常任务

php - 无法使用 Eloquent ORM 更新 mysql 行

django - 将 Django 字段描述从现有模型复制到新模型