【问题标题】:django does not parse {{ title }}django 不解析 {{ title }}
【发布时间】: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


【解决方案1】:

正如 Matthias 在评论中所说,看起来你根本没有真正调用 Django - 这些是正常的 Apache 目录索引(所以自然 Django 不会解析模板,因为它甚至没有被调用)。

由于您刚刚开始,您根本不应该使用 Apache,而是按照教程中的说明启动本地开发服务器 (./manage.py runserver)。

【讨论】:

  • 现在我明白你的意思了。这些是普通的 Apache 目录索引。我应该怎么做才能调用 Django?我应该去哪里寻找问题?
  • 既然(根据您上面的评论)您已经在端口 8000 上启动了服务器,您应该转到 domain.com:8000
【解决方案2】:

试试这个:

进口:

from django.shortcuts import render_to_response, RequestContext

Views.py:

 return render_to_response('index.html',  {'title' :Your_variable_name}, context_instance=RequestContext(request))

在模板中这样调用:

{{ title }}

【讨论】:

  • 我已经按照您的解释更改了 views.py,但没有任何改变。我像这样'domain/mysite/blog/templates'进入我的浏览器,然后我看到了我的'index.html'。当我去domain/mysite 时,我看到“/mysite 的索引”。我做错了吗?
  • 你的安装,一切都很好。在您的 settings.py 中,您必须在 TEMPLATE_DIRS 中提供模板目录的完整路径。另一件事,使用 django 开发服务器。它很容易使用。在你有信心之后,使用带有 mod_wsgi 模块的 apache 服务器将你的 Django 部署到生产服务器中。
  • 我找到了 TEMPLATE_DIRS 并写了TEMPLATE_DIRS = ( "blog/templates",,它没有任何效果,正如我所料。我认为我的问题是,正如@Matthias 和@Daniel Roseman 所说,这些目录只是由 Apache 提供的。我不能调用 Django,在这一点上,我不知道。而且每个人都说我应该在本地工作,是让Django在生产服务器上工作真的很难,还是因为我是新手?
  • 不,在生产服务器中使用 Django 并不难。但首先要熟悉 Django 本身。使用 Django 开发服务器并在本地运行。在 TEMPLATE_DIRS 中给出模板目录的完整路径并运行开发服务器。一切都会好起来的。
【解决方案3】:

您应该使用render 快捷方式。

views.py

from django.shortcuts import render

def my_view(request):
    return render(request, 'index.htm', {'title': 'Hey!'})

这具有将请求实例添加到强制使用 RequestContext 的上下文中的额外优势。在这里阅读:https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render

【讨论】:

    猜你喜欢
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2020-03-14
    • 2018-07-07
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多