【问题标题】:Django deployment, all links are prepended with index.wsgiDjango 部署,所有链接都以 index.wsgi 开头
【发布时间】:2014-02-02 12:39:12
【问题描述】:

我想在带有 Apache 和 mod_wsgi 的 Ubuntu 服务器中部署我的 django 应用程序。我正在关注本教程 http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/

在我的网站文件中,我有

<VirtualHost *:80>
    ServerAdmin webmaster@mydomain.com
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    WSGIScriptAlias / var/www/index.wsgi

    Alias /static/ /var/www/static/
    <Location "/static/">
        Options -Indexes
    </Location >
</VirtualHost >

不要介意通用的 mydomain.com 条目。该站点仅用于显示进度,因此我可以与朋友讨论。

在 /var/www/index.wsgi 我有这个

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/voger/venvs/ssite/local/lib/python2.7/site-packages')



# Add the app's directory to the PYTHONPATH
sys.path.append('/home/voger/projects/ssite')
sys.path.append('/home/voger/projects/ssite/ssite')

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

# Activate your virtual env
activate_env=os.path.expanduser("/home/voger/venvs/ssite/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

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

在我的 settings.py 中,我有两行用于登录和注销后的重定向。这些没有以 index.wsgi 开头

LOGIN_REDIRECT_URL = '/login_redirect'
ACCOUNT_LOGOUT_REDIRECT_URL = "/logout_redirect"

当请求这些 url 时,它返回 404

Reverse for 'login_redirect' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

在我的 html 中有 4 个链接

<a id="logout" style="display: inline-block;" href="/index.wsgi/accounts/logout/">Logout</a>
<a id="profile" style="display: inline-block;" href="/accounts/profile">My Account</a>
<a id="signup" style="display: none;" href="/index.wsgi/accounts/signup/">Sign Up</a>
<a id="login" style="display: none;" href="/index.wsgi/accounts/login/">Sign In</a>

正如您所见,使用 {% url %} 插入的 3 个条目会自动添加到 /index.wsgi 前面,而唯一硬编码的条目没有添加到前面。但它们仍然按预期工作。

为什么我有这个 /index.wsgi 前置,我怎样才能删除它?在我使用 django 内置服务器的开发机器中,一切正常。

我不知道是否相关,但登录、注销和注册 url 由 django-allauth 提供。

感谢您的任何回答或评论

【问题讨论】:

  • 请使用official documentation,如果您仍有问题,请更新问题。排除随机博客文章中的错误是不切实际的。

标签: python django apache mod-wsgi


【解决方案1】:

对于初学者:

WSGIScriptAlias / var/www/index.wsgi

错了。您在 WSGI 脚本的路径上缺少前导斜杠。

您也不应该像您那样将 index.wsgi 粘贴在 DocumentRoot 目录中。如果由于某种原因没有使用带有 WSGIScriptAlias 的 VirualHost,并且 wsgi-script 的 .wsgi 处理程序已在 Apache 配置的其他位置设置,则意味着 /index.wsgi 的 URL 可以使用您的所有然后从那里开始的问题。这是因为 Django 会认为它是在 URL 前面加上 /index.wsgi,因为它最终会成为应用程序的挂载点。

因此,将 WSGI 脚本文件移出服务器的 DocumentRoot 目录。删除 .wsgi 扩展名的任何 AddHandler 指令。确保您的 VirtualHost 实际正在被使用,特别是 WSGIScriptAlias 指令的使用是正确的。

顺便说一句,你为什么使用一些随意的人的博客文章而不是 Django 文档网站上的官方 mod_wsgi 设置说明。

【讨论】:

  • 谢谢。我采用了官方文档的方式,现在它可以工作了。我使用了博客文章,因为当我第一次尝试它时它工作并且它不使用 httpd.conf 但 /etc/apache2/sites-available 所以也许这将允许我将来在该服务器中添加更多站点。
猜你喜欢
  • 2012-03-09
  • 2014-07-04
  • 2012-03-03
  • 2021-12-08
  • 1970-01-01
  • 2018-04-25
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多