【发布时间】:2013-03-20 00:19:23
【问题描述】:
我在 settings.py 中找不到 TEMPLATE_DIR,无法知道我的模板文件在哪里。
另外,当我转到我的 index.html(您可以在下面看到)时,解析不起作用(例如 {{ title }},您可以在下面的 views. py )。
为什么它({{title }})不工作?
我认为我已经很接近让它工作了,但我做不到。
另外,我在 SO 中找不到任何相关主题。
所以,我想看到的是:
但我看到了这个:
index.html 看起来像:
index.html 来源:
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<div class="container">
<h1>First Blog </h1>
<h2>{{ title }}</h2>
<h3>Posted on {{ date }} by {{ author }}</h3>
<p>{{ body }}</p>
</div>
</body>
</html>
view.py 看起来像这样:
# Create your views here.
from django.shortcuts import render_to_response
from blog.models import posts
def home(request):
return render_to_response('index.html' {'title :'My First Post'})
~
models.py 看起来像这样:
from django.db import models
# Create your models here.
class posts(models.Model):
author = models.CharField(max_lenght = 30)
title = models.CharField(max_lenght = 100)
bodytext = models.TextField()
timestamp = models.DateTimeField()
这是我之前所做的:
已安装 Python 2.7.2
user@domain.com [~]# python -V
Python 2.7.2
已安装 Django 1.5
user@domain.com [~]# python
Python 2.7.2 (default, Mar 27 2013, 12:07:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django import get_version
>>> get_version()
'1.5'
我had problems about creating my project (django-admin.py startpriject mysite),
username@domain.com [~/www/domain/]# django-admin.py startproject mysite
-bash: django-admin.py: command not found
这是一个 PATH 问题,最后是solved,感谢 SO,我已经成功创建了我的项目。
然后我像这样修改models.py(创建一个简单的博客):
from django.db import models
# Create your models here.
class posts(models.Model):
author = models.CharField(max_lenght = 30)
title = models.CharField(max_lenght = 100)
bodytext = models.TextField()
timestamp = models.DateTimeField()
已安装 MySQL-python 1.2.4
username@domain.com [~]# python
Python 2.7.2 (default, Mar 27 2013, 12:07:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
我创建了一个 mysql 数据库 和一个 用户,将用户添加到数据库并授予所有权限(全部在 bluehost 前端面板中完成)。
我had problems about database update,
python manage.py syncdb
感谢 SO,that has been also solved。
user@domain.com [~/www/domain/mysite]# python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user Creating table django_content_type
Creating table django_session
Creating table django_site
You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no):
为什么它({{title }})不工作?
我应该改变什么?
我应该重新安装一些东西吗?
【问题讨论】:
-
return render_to_response('index.html' {'title :'My First Post'})看起来不对。请尝试return render_to_response('index.html', {'title': 'My First Post'})。 -
哦,我更换了线路,但没有任何改变。
# Create your views here. from django.shortcuts import render_to_response from blog.models import posts def home(request): return render_to_response('index.html', {'title': 'My First Post'}) -
你启动服务器了吗?看起来目录只是由 Apache 提供,而不是由 Django 后端提供。
-
我认为这可能是我的问题。我不知道,我应该在什么时候启动服务器。我按照@AlexanderAfanasiev 所说更改了我的代码,像这样启动服务器:
python manage.py runserver 0.0.0.0:8000,现在,我不知道该去哪里检查。我的目录是这样的domain.com/mysite/(主项目)和domain.com/mysite/blog/(应用程序),而我的index.html在domain.com/mysite/blog/templates/index.html,我应该去哪个url?
标签: python django parsing python-2.7 django-templates