使用 URL 前缀(“子目录")运行 Django - 应用程序有效,但 URL 损坏? [英] Run Django with URL prefix ("subdirectory") - App works, but URLs broken?

查看:18
本文介绍了使用 URL 前缀(“子目录")运行 Django - 应用程序有效,但 URL 损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想在前缀/d"处为 Django/apps 提供服务,因此example.com/d/"将加载默认应用,example.com/d/app3"将按照配置加载另一个在 urls.py 中.

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

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

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

有什么建议吗?

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

--- 配置如下:

<预><代码>##/home/blah/django_projects/Ticket/urls.py#从 django.conf.urls.defaults 导入 *从 django.contrib 导入管理员admin.autodiscover()urlpatterns = 模式('',(r'^', include('ticket.urls')),(r'^admin/', 包括(admin.site.urls)),(r'^comments/', include('django.contrib.comments.urls')),)##/home/blah/django_projects/Ticket/apache/django.wsgi#导入操作系统,系统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'导入 django.core.handlers.wsgi应用程序 = django.core.handlers.wsgi.WSGIHandler()##/etc/apache2/sites-available/django_tickets_wsgi (apache conf)#名称虚拟主机 *<虚拟主机 *>别名/media/home/blah/django_projects/Tickets/mediaWSGIScriptAlias/d/home/blah/django_projects/Tickets/apache/django.wsgiWSGIDaemonProcess exmaple_com user=blah group=blah processes=1 线程=10WSGIProcessGroup example_com服务器管理员 localhost@example.com服务器名称 example.com文档根目录/var/www/<目录/var/www/>选项 -Indexes FollowSymLinks -MultiViews -Includes允许覆盖无命令允许,拒绝允许所有人</目录>ErrorLog/var/log/apache2/error.log# 可能的值包括:调试、信息、通知、警告、错误、暴击、# 警报,紧急.日志级别警告CustomLog/var/log/apache2/access.log 合并服务器签名关闭</虚拟主机>

解决方案

这可能是 Django 的副本Apache 重定向问题,因为该答案解决了这个问题.

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

答案:(通过alex vasi)

要尝试的事情:

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

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

Below are the relevant configuration files, also at 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.

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.

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

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'.

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

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.

--- 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 localhost@example.com
    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>

解决方案

This is a possible duplicate of Django Apache Redirect Problem, as that answer solved this 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/'

这篇关于使用 URL 前缀(“子目录")运行 Django - 应用程序有效,但 URL 损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆