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.
With urls.py setup such that:
url("^about_us/$", views.about_us, name="about_us"),
In production, I am getting the link www.mysite.com/home/username/myapp/about_us instead of www.mysite.com/about_us
I have looked at this similar issue and it does not help with my specific application: Django url tag adds filepath
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
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)
I feel like something is off in one of these files. How can I get remove the /home/user/myapp from my links?
Update: I thought I figured out this problem. When I change the SCRIPT_NAME variable in passenger_wsgi.py to
SCRIPT_NAME = '.'
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
Maybe this will provide some direction to the solution.
Update #2: I did find this: https://smartlazycoding.com/django-tutorial/deploy-a-django-website-to-a2-hosting
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()
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.