nginx:django模板index.html无法很好地显示

时间:2015-11-18 10:18:10

标签: django nginx

我有一个Django项目,其主要结构如下

proj/
  + main/
     + templates/
     |  + index.html
     |  + header.html
     |  + navbar.html
     |  + ...
     + other files
     + ...
  + another_app/
  + ...

index.html包括navbar.htmlnavbar.html。 然后我用gunicorn和nginx公开服务。但是,索引页面无法正常显示。它包含Django模板语法,如下所示:

{% include 'header.html' %} {% include 'navbar.html' %}
Title

我不太清楚为什么会这样。

这是该项目的Nginx配置文件:

server {
    listen 80;
    server_name proj.app;
    root "/home/vagrant/proj/main";

    index index.html index.htm;

    charset utf-8;

    location / {
        root "/home/vagrant/proj/main/templates";
        try_files $uri $uri/ /index.html /index.htm;
     }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/proj.app-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location /api {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:8000;
    }

    location /static {
        alias "/home/vagrant/proj/main/static";
    }

    location /media  {
        alias "/home/vagrant/proj/main/media";
    }

    location ~ /\.ht {
        deny all;
    }
}

我曾尝试使用上游模型或在proxy_pass时使用服务器gunicorn.sock,但这仍然无效。有人能给我一个关于这个问题的暗示吗?非常感谢。

1 个答案:

答案 0 :(得分:4)

您已将nginx配置为直接提供模板。我不确定你为什么这样做,但显然这样做会完全绕过Django,所以模板将作为原始HTML提供,不会被解析。

解决方案:不要这样做。