Django URL模板标签添加了绝对文件路径 [英] Django url template tags adding absolute filepath
问题描述
我遇到了一个问题,我的{%url%} django模板标记正在将文件路径添加到生产环境中的网址.这不会复制到我的本地开发计算机上.
I am having a problem where my {% url %} django template tag is adding the filepath to the web address in production. This is not replicated on my local development machine.
使用urls.py设置,例如:
With urls.py setup such that:
url("^about_us/$", views.about_us, name="about_us"),
在生产中,我正在获取链接 www.mysite.com/home/username/myapp/about_us 代替 www.mysite.com/about_us
In production, I am getting the link www.mysite.com/home/username/myapp/about_us instead of www.mysite.com/about_us
我看过这个类似的问题,它对我的特定应用程序无济于事: Django url标签添加文件路径
I have looked at this similar issue and it does not help with my specific application: Django url tag adds filepath
我的django项目托管在使用Apache,wsgi和passenger的A2(共享)托管上.我的.htaccess文件如下所示:
My django project is hosted on A2 (shared) hosting using apache, wsgi, and passenger. My .htaccess file look the following:
# 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文件如下所示:
My passenger_wsgi.py file looks like the following:
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?
I feel like something is off in one of these files. How can I get remove the /home/user/myapp from my links?
更新:我想我已经解决了这个问题.当我将passenger_wsgi.py中的SCRIPT_NAME变量更改为
Update: I thought I figured out this problem. When I change the SCRIPT_NAME variable in passenger_wsgi.py to
SCRIPT_NAME = '.'
这最初可以在我转到主页时解决此问题.但是,一个新问题导致,如果您访问www.mysite.com/about_us,则{%url%}标签将导致
This initially fixes the problem when I go to the home page. However, a new problem results in that if you go to www.mysite.com/about_us, then the {% url %} tag will result in
www.mysite.com/about_us/about_us
www.mysite.com/about_us/about_us
也许这将为解决方案提供一些指导.
Maybe this will provide some direction to the solution.
更新#2 :我确实发现了这一点: https://smartlazycoding.com/django-tutorial/将django网站部署到a2托管
Update #2: I did find this: https://smartlazycoding.com/django-tutorial/deploy-a-django-website-to-a2-hosting
在此网站上,将passenger_wsgi.py文件更改为
From this website, changing the passenger_wsgi.py file to
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问题.然后,我进行了网站建议的更改:
fixes the issue, but then you still have the POST issue it describes. I then made the changes the website recommends:
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)
这导致了我原来的问题.
This results back into my original problem.
推荐答案
我有一个目前似乎有效的答案.解决方案基本上是上面发布的原始passenger_wsgi.py文件,但是将SCRIPT_NAME设置为一个空字符串.我的passenger_wsgi.py文件如下所示:
I have an answer that appears to work for now. The solution is basically the original passenger_wsgi.py file posted above, but setting SCRIPT_NAME equal to an empty string. My passenger_wsgi.py file looks like the following:
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 =''的警告,但是如果有人知道这样做的任何问题,请分享.
This fixes my path issue and I am able to use POST. I don't know if there are any caveats of setting SCRIPT_NAME = '' over the app's root path, but if anyone knows of any issues of doing this, please share.
这篇关于Django URL模板标签添加了绝对文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!