【发布时间】:2015-11-18 10:18:09
【问题描述】:
我有一个 Django 项目,它的主要结构是这样的
proj/
+ main/
+ templates/
| + index.html
| + header.html
| + navbar.html
| + ...
+ other files
+ ...
+ another_app/
+ ...
而index.html 包括navbar.html 和navbar.html。
然后我使用 gunicorn 和 nginx 将其公开。但是,索引页面无法正确显示。它包含这样的 Django 模板语法:
{% include 'header.html' %} {% include 'navbar.html' %}
Title
我不太清楚为什么会这样。
这是我的项目的 Nginx conf:
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,但这仍然不起作用。有人可以给我一个关于这个问题的提示吗?非常感谢。
【问题讨论】:
-
我觉得我在这里遗漏了一些重要的东西。为什么您的网络服务器配置与您的模板文件问题相关?您的网络服务器应该(几乎)从不直接为您的模板文件提供服务,它们应该由 Django 处理。
-
是的,这可能就是重点。我正在努力解决这个问题。