Django URL模板标签添加了绝对文件路径

时间:2018-12-28 16:58:58

标签: python django apache passenger wsgi

我遇到了一个问题,我的{%url%} django模板标记正在将文件路径添加到生产环境中的网址。这不会复制到我的本地开发计算机上。

使用urls.py设置,例如:

url("^about_us/$", views.about_us, name="about_us"),

在生产中,我正在获取链接 www.mysite.com/home/username/myapp/about_us 代替 www.mysite.com/about_us

我看过这个类似的问题,它对我的​​特定应用程序没有帮助: Django url tag adds filepath

我的django项目托管在使用Apache,wsgi和passenger的A2(共享)托管上。我的.htaccess文件如下所示:

# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN 
PassengerAppRoot "/home/user/myapp"
PassengerBaseURI "/"
PassengerPython "/home/user/virtualenv/myapp/2.7/bin/python2.7"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END

我的passenger_wsgi.py文件如下所示:

import myapp.wsgi
SCRIPT_NAME = '/home/user/myapp'

class PassengerPathInfoFix(object):
    """
    Sets PATH_INFO from REQUEST_URI because Passenger doesn't provide it.
    """
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        from urlparse import unquote
        environ['SCRIPT_NAME'] = SCRIPT_NAME

        request_uri = unquote(environ['REQUEST_URI'])
        script_name = unquote(environ.get('SCRIPT_NAME', ''))
        offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
        environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
        return self.app(environ, start_response)

application = myapp.wsgi.application
application = PassengerPathInfoFix(application)

我感觉这些文件之一中的某些内容有问题。如何从链接中删除/ home / user / myapp?

更新:我想我已经解决了这个问题。当我将passenger_wsgi.py中的SCRIPT_NAME变量更改为

SCRIPT_NAME = '.'

这最初解决了我转到主页时出现的问题。但是,一个新问题导致,如果您访问www.mysite.com/about_us,则{%url%}标签将导致

www.mysite.com/about_us/about_us

也许这将为解决方案提供一些指导。

更新#2 :我确实找到了这个: https://smartlazycoding.com/django-tutorial/deploy-a-django-website-to-a2-hosting

在此网站上,将passenger_wsgi.py文件更改为

import os
import sys
# set variables
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'
#setup django application
import django.core.handlers.wsgi
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

解决了该问题,但是您仍然遇到它描述的POST问题。然后,我进行了网站建议的更改:

import os
import sys
# Set up paths and environment variables
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'
import django.core.handlers.wsgi
from django.core.wsgi import get_wsgi_application
SCRIPT_NAME = os.getcwd()
class PassengerPathInfoFix(object):
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        from urllib import unquote
        environ['SCRIPT_NAME'] = SCRIPT_NAME
        request_uri = unquote(environ['REQUEST_URI'])
        script_name = unquote(environ.get('SCRIPT_NAME', ''))
        offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
        environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
        return self.app(environ, start_response)
application = get_wsgi_application()
application = PassengerPathInfoFix(application)

这导致了我原来的问题。

1 个答案:

答案 0 :(得分:0)

我有一个似乎暂时有效的答案。解决方案基本上是上面发布的原始passenger_wsgi.py文件,但是将SCRIPT_NAME设置为一个空字符串。我的passenger_wsgi.py文件如下所示:

import myapp.wsgi
#SCRIPT_NAME = '/home/user/myapp'
SCRIPT_NAME = ''

class PassengerPathInfoFix(object):
    """
    Sets PATH_INFO from REQUEST_URI because Passenger doesn't provide it.
    """
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        from urllib import unquote
        environ['SCRIPT_NAME'] = SCRIPT_NAME

        request_uri = unquote(environ['REQUEST_URI'])
        script_name = unquote(environ.get('SCRIPT_NAME', ''))
        offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
        environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
        return self.app(environ, start_response)

application = myapp.wsgi.application
application = PassengerPathInfoFix(application)

这可以解决我的路径问题,并且可以使用POST。我不知道在应用程序的根路径上设置SCRIPT_NAME =''是否有任何警告,但是如果有人知道这样做的任何问题,请分享。