apache 使用 URL 前缀(“子目录”)运行 Django - 应用程序有效,但 URL 损坏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1466708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:11:12  来源:igfitidea点击:

Run Django with URL prefix ("subdirectory") - App works, but URLs broken?

djangoapachemod-wsgi

提问by anonymous coward

Below are the relevant configuration files, also at http://dpaste.com/97213/.

下面是相关的配置文件,也在http://dpaste.com/97213/

The apache config is currently working, because accessing 'example.com/' shows me the index.html file I have placed at the document root.

apache 配置当前正在运行,因为访问“example.com/”会显示我放置在文档根目录下的 index.html 文件。

I'd like to serve Django/apps at the prefix '/d', so 'example.com/d/' would load the default app, 'example.com/d/app3' would load another, as configured in urls.py.

我想在前缀“/d”处为 Django/apps 提供服务,因此“example.com/d/”将加载默认应用程序,“example.com/d/app3”将加载另一个,如 urls 中配置的那样。 py。

Serving Django, I'm using the suggested mod_wsgi, on Linux.

为 Django 服务,我在 Linux 上使用建议的 mod_wsgi。

Currently, I can access the Ticket app at 'example.com/d', but when the @login_required decorator tries to send me to the login page, I get sent to 'example.com/accounts/login', rather than the expected 'example.com/d/accounts/login'.

目前,我可以在“example.com/d”访问Ticket应用,但是当@login_required装饰器尝试将我发送到登录页面时,我被发送到“example.com/accounts/login”,而不是预期的'example.com/d/accounts/login'。

Since the default app loads correctly, I'm not sure what I'mdoing wrong here, or if this is a bug in Django when generating the urls.

由于默认应用程序正确加载,我不确定我在这里做错了什么,或者这是否是 Django 在生成 url 时的错误。

Any suggestions?

有什么建议?

EDIT:As a note, if I change the apache config line: WSGIScriptAlias /d /home/blah/django_projects/Tickets/apache/django.wsgi to WSGIScriptAlias / /home/blah/django_projects/Tickets/apache/django.wsgi The application, commenting, and logging in all work fine. Even going to 'example.com/admin' loads the admin, although I've left the admin media broken, so no stylesheets are loaded.

编辑:作为说明,如果我更改 apache 配置行: WSGIScriptAlias /d /home/blah/django_projects/Tickets/apache/django.wsgi 到 WSGIScriptAlias / /home/blah/django_projects/Tickets/apache/django.wsgi 应用程序、评论和登录一切正常。即使去'example.com/admin' 也会加载管理员,尽管我已经把管理媒体弄坏了,所以没有加载样式表。

--- Configs Follow:

--- 配置如下:

#
# /home/blah/django_projects/Ticket/urls.py
#
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^', include('ticket.urls')),
    (r'^admin/', include(admin.site.urls)),
    (r'^comments/', include('django.contrib.comments.urls')),
)


#
# /home/blah/django_projects/Ticket/apache/django.wsgi
#
import os, sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')

sys.path.append('/home/blah/django_projects')
sys.path.append('/home/blah/django_projects/Tickets')

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

import django.core.handlers.wsgi

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


#
# /etc/apache2/sites-available/django_tickets_wsgi (apache conf)
#
NameVirtualHost *
<VirtualHost *>  

    Alias /media /home/blah/django_projects/Tickets/media


    WSGIScriptAlias /d /home/blah/django_projects/Tickets/apache/django.wsgi
    WSGIDaemonProcess exmaple_com user=blah group=blah processes=1 threads=10
    WSGIProcessGroup example_com

    ServerAdmin [email protected]
    ServerName example.com

    DocumentRoot /var/www/

    <Directory /var/www/>
        Options -Indexes FollowSymLinks -MultiViews -Includes 
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

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

    CustomLog /var/log/apache2/access.log combined
    ServerSignature Off

</VirtualHost>

采纳答案by anonymous coward

This is a possible duplicate of Django Apache Redirect Problem, as that answer solved this problem.

这可能是Django Apache Redirect Problem 的副本,因为该答案解决了这个问题

I only eventually stumbled on that answer by opening almost all of the 'related questions' here, just out of desperation. From my perspective, I think my question has some valuable "search friendly" words.

我最终只是出于绝望,在这里打开了几乎所有“相关问题”,偶然发现了这个答案。从我的角度来看,我认为我的问题有一些有价值的“搜索友好”词。

EDIT:The answer: (via alex vasi)

编辑:答案:(通过亚历克斯瓦西

Things to try:

尝试的事情:

  1. Change current domain to "yourdomain.tld/cflow" in the "sites" framework. It's easy to do using django admin or dumpdata/loaddata manage.py commands.
  2. Looks like your site is using login_required decorator. In that particular case you can add to settings.py:

    LOGIN_URL = '/[prefix]/accounts/login/'

  1. 在“站点”框架中将当前域更改为“yourdomain.tld/cflow”。使用 django admin 或 dumpdata/loaddata manage.py 命令很容易做到。
  2. 看起来您的网站正在使用 login_required 装饰器。在这种特殊情况下,您可以添加到 settings.py:

    LOGIN_URL = '/ [前缀]/accounts/login/'

回答by Ahmed Elhamidi

In your urls.py rename urlpatterns to base_urlpatterns; then add the followinig definition at the end of the same file:

在您的 urls.py 中,将 urlpatterns 重命名为 base_urlpatterns;然后在同一文件的末尾添加以下定义:

urlpatterns = patterns('',
    '^', include(base_urlpatterns), # iff you wish to maintain the un-prefixed URL's too
    '^your_prefix/', include(base_urlpatterns),
)