【问题标题】:django apache deployment serve django templates as static htmldjango apache 部署将 django 模板作为静态 html 服务
【发布时间】:2014-05-13 04:16:27
【问题描述】:

我的 django apache 部署以静态 html 形式提供 django 模板

我的网页上出现以下标签

{% if cmd %} {{ cmd }} {% endif %} {% if command %} {{ command}}
Running python script baby....wait...

{% endif %} {% if documents %}
{% for document in documents %} {% endfor %}
{% else %}
No documents.

{% endif %} {% csrf_token %}
{{ form.non_field_errors }}

我的部署配置有问题吗

我在我的语言环境 ubuntu 机器上使用 Apache2 进行部署。

【问题讨论】:

  • 您的模板目录在 settings.py 文件中的外观如何?
  • TEMPLETE_DIRS = ('/var/www/TEServices/templates', )
  • 如果您没有提供 Apache 配置的任何详细信息、您使用的 URL、您的 Django urls.py 和视图代码或任何其他相关信息,我们应该如何诊断您的 Apache 设置?

标签: python django apache deployment


【解决方案1】:

我的部署设置: 项目结构:

sites/
└── motivate-me.local
    ├── project
    │   ├── api
    │   │   ├── __pycache__
    │   │   └── v1
    │   │       └── __pycache__
    │   └── project
    │       └── __pycache__
    └── static
        ├── admin
        │   ├── css
        │   ├── img
        │   │   └── gis
        │   └── js
        │       └── admin
        └── rest_framework
            ├── css
            ├── img
            └── js

django.wsgi:

import os
import sys

sys.path.append('/home/mikhail/sites/motivate-me.local/')
sys.path.append('/home/mikhail/sites/motivate-me.local/project/')
sys.path.append('/usr/local/lib/python3.3/dist-packages')

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

我的主人:

127.0.0.1   localhost
127.0.0.2   www
127.0.0.3   loss-weight.local
127.0.0.4   test.local
127.0.0.5   motivate-me.local
127.0.1.1   ube-work

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

我的虚拟主机:

<VirtualHost 127.0.0.5:80>
ServerAdmin webmaster@motivate-me.local
ServerName motivate-me.local
ServerAlias motivate-me.local

WSGIScriptAlias / /home/mikhail/sites/motivate-me.local/project.wsgi
WSGIPassAuthorization on

DocumentRoot /home/mikhail/sites/motivate-me.local
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /home/mikhail/sites/motivate-me.local/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

Alias /static/ /home/mikhail/sites/motivate-me.local/static/
<Location "/static/">
    Options -Indexes
</Location>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

【讨论】:

    【解决方案2】:

    最后我得到了它与下面的细节一起工作

    为您的项目创建一个 .wsgi 文件(如 mysite.wsgi),并在项目内部的任何位置包含以下信息

    import os
    import sys
    path = '/home/synerzip/Documents/pavanWork/mysite'
    if path not in sys.path:
        sys.path.append(path)
    os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

    在 /etc/apache2/sites-available/ 中创建一个 .conf 文件

    <VirtualHost *:80>
    WSGIScriptAlias /  /path/to/.wsgi                    # file which is created in above step
    ServerName mysite.com                                       # Name of your site
    Alias /static/  /path/to/your/project /static/files
    <Directory /path/to/your/project />
    Order allow,deny
    Allow from all
    </Directory> 
    </VirtualHost>
    

    重启apache服务器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 2012-08-08
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多