【问题标题】:django's HttpResponse is plain text and not HTMLdjango 的 HttpResponse 是纯文本而不是 HTML
【发布时间】:2014-02-09 09:54:32
【问题描述】:

我是 Django 新手。我现在正在学习它几个星期。我有一个 .txt 文件,其中包含我想要呈现的 HTML 内容。我知道我没有正确链接 css 和 js 文件。你能帮忙吗?

项目结构:

mysite/
   manage.py
   static/
      css/
        home.css
      images/
        logo.png
   templates/
      indexTemplate.txt

indexTemplate.txt

<html>
<head>
    <title>Home Page</title>
    <link rel="stylesheet" href="{% static "css/home.css" %}"/>
    <script src="{% static "js/jquery-1.10.2.js" %}"></script>
</head>
<body>
    <div class="Header">

        <div class="HeaderLogo">
            <a href="index.html"><img src="{% static "images/logo.png" %}" alt="Logo"/></a>
        </div>
    Nav1 | Nav 2 
    </div>
</body>
</html>

settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

SECRET_KEY = 'u^37+&l60(xhoq8qyzlsyvynynpx%i-amg@@n#_x)tugfc5)61'

DEBUG = True

TEMPLATE_DEBUG = True

TEMPLATE_DIRS = (
    '/home/ubuntu/Desktop/mysite/templates',
)
ALLOWED_HOSTS = []

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'

DATABASES = {
'default': {
    'ENGINE':'django.db.backends.mysql',
    'NAME': 'myapp',
    'USER': 'root',
    'PASSWORD': '***',
    'HOST': '*********',
    'PORT': '8000',
}
}

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'`

views.py:

from django.http import HttpResponse
from django.template import Template, Context
from django.template.loader import get_template
from django.shortcuts import render_to_response

def hello(request):
    t = get_template('indexTemplate.txt')
    html = t.render(Context({'mnu':'Trending'}))
    return HttpResponse(html)

【问题讨论】:

  • 我在 EC2 实例中执行此操作,如果这很重要的话。我现在已经从模板中取出所有东西并只渲染一个图像。我已将 settings.py 中的静态位置从 /static/ 更改为 /home/ubuntu/Desktop/mysite/static/ 。现在我的响应页面的视图源是 STILL 图片没有显示,只显示了alt文本。我哪里错了? :(

标签: django


【解决方案1】:

你需要

{% load static %}

在您的模板中,靠近顶部的某个位置,{% static ... 标记之前。

然后在你的 settings.py 中添加

STATICFILES_DIRS = (
    '/home/ubuntu/Desktop/mysite/static',
)

确保这确实是正确的路径。

【讨论】:

  • 嘿,谢谢。将此添加为模板文件中的第一行。我仍然看到纯文本,没有格式。这是 runserver 控制台所说的:09/Feb/2014 11:06:58] "GET /static/css/home.css HTTP/1.1" 404 1672 [09/Feb/2014 11:06:58] "GET /静态/js/jquery-1.10.2.js HTTP/1.1" 404 1693 [09/Feb/2014 11:06:58] "GET /static/js/ShuffleAndDraw.js HTTP/1.1" 404 1696 [09/Feb/ 2014 11:06:59]“GET /static/images/LogoV2.png HTTP/1.1”404 1687
  • 嘿抱歉,我错过了一个东西,请参阅编辑后的答案。但是,当您说看到“纯文本”时,您的意思是正常渲染的 html 只是没有样式,还是您实际上看到的是纯文本(在浏览器中查看页面时是否看到 html 标签)?
  • 另外,不要更改STATIC_URL,这很好,你只是在该行的末尾多了一个`(也许这里只是一个错字?)
  • 我觉得应该是{% load staticfiles %}
  • 如果他不使用 S3,{% load static %} 应该可以工作。但是由于他处于调试模式,所以这无关紧要,因为他只是将未收集到的静态文件提供给生产存储。内置的static 标签与django.contrib.staticfiles 配合得很好。 OTOH,使用{% load static from staticfiles %} 也没有缺点,所以他使用DEBUG = True 的哪一个并不重要。
猜你喜欢
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多