我一直在遵循本教程: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
谢谢!

最佳答案

模板中的变量称为“帖子”

<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/

10-12 16:36
查看更多