【问题标题】:Template Tags Not Loading in Apache模板标签未在 Apache 中加载
【发布时间】:2016-03-26 17:37:53
【问题描述】:

所以我有一个非常基本的 Django 应用程序。我已经使用开发服务器部署了应用程序,我的页面似乎完全没有任何问题。但是,一旦我尝试在 Apache 服务器上运行它,HTML 页面就会提供服务,但底层模板标签却没有。

下面是我的 Apache 配置:

WSGIScriptAlias / /home/ec2-user/website/bash/wsgi.py
WSGIPythonPath /home/ec2-user/website
<Directory "/home/ec2-user/website/bash/">
    <Files wsgi.py>
            Require all granted
    </Files>
</Directory>

Alias /static/ /home/ec2-user/website/static

<Directory "/home/ec2-user/website/static">
    Require all granted
</Directory>

我使用的唯一模板标签是提供静态文件的标签。这是我的 settings.py 配置:

import os

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

# Application definition

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

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'home/','static/'), '/static/')

SITE_MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'home/', 'templates/', 'home')

我知道它没有呈现,因为我在 Apache 中的访问日志只是显示模板标签的转义字符键值。

[20/Dec/2015:14:00:37 +0000] "GET /%7B%%20static HTTP/1.1" 400 226
[20/Dec/2015:14:00:38 +0000] "GET /%7B%%20static HTTP/1.1" 400 226
[20/Dec/2015:14:05:08 +0000] "GET /%7B%%20static HTTP/1.1" 400 226
[20/Dec/2015:14:05:09 +0000] "GET /%7B%%20static HTTP/1.1" 400 226
[20/Dec/2015:14:19:10 +0000] "GET /%7B%%20static HTTP/1.1" 400 226
[20/Dec/2015:14:19:12 +0000] "GET /%7B%%20static HTTP/1.1" 400 226
[20/Dec/2015:14:25:35 +0000] "GET /%7B%%20static HTTP/1.1" 400 226
[20/Dec/2015:14:25:36 +0000] "GET /%7B%%20static HTTP/1.1" 400 226
[20/Dec/2015:14:25:49 +0000] "GET /%7B%%20static HTTP/1.1" 400 226
[20/Dec/2015:14:25:50 +0000] "GET /%7B%%20static HTTP/1.1" 400 226

这是我的 HTML 模板代码,请不要介意糟糕的代码!

<!DOCTYPE html>
<html>

    <head>
        {% load staticfiles %}

        <title> Home </title>
        <link href="{% static "css/style_file.css" %}" type = "text/css" rel = "stylesheet" />
        <link rel="icon" type="image/png" href="{% static "media/hat.png" %}" />
    </head>

    <body>


        <div class="container">


                <div id="logo"> <img src="{% static "media/title.png" %}" alt="title"></div>
        </div>

    </body>
</html>

我完全迷失了,不知道我哪里出错了。我知道我通常可以正常提供页面,因为在开发服务器中,视图功能正在按预期提供模板。我在 Apache 服务器上做错了什么!

编辑:进一步检查,这个问题似乎只存在于端口 80 上。即使我在该端口上使用开发服务器,它也不会加载相应的标签和/或静态元素。

【问题讨论】:

  • 而且只是添加它似乎只发生在端口 80 上。当我在任何其他端口上运行开发服务器时,它都会呈现,只有端口 80 会出现这些呈现问题。
  • 您可以编辑您的描述而不是添加 cmets,以便每个人都清楚问题。
  • 我很惊讶您收到 400 响应。您是否在设置中设置了ALLOWED_HOSTS?
  • 我确实将它设置为 '*'。
  • 当你看到这个时,你要去哪个网址?你项目的 urls.py 是什么样的?

标签: python html django apache django-templates


【解决方案1】:

更改所有权和最小的语法错误。在“/home/ec2-user/website/static**/**”之后需要一个斜杠。

【讨论】:

    猜你喜欢
    • 2019-12-30
    • 2017-07-29
    • 2016-05-05
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2012-01-26
    • 2010-11-15
    相关资源
    最近更新 更多