I have a Django site and I have set up a test server for it. Now, the test server is being used for something else (php) also and I have configured it as aliases in virtual host config for Apache.
Now the problem is this:
When I open it as: http://x.x.x.x/djangoSite/
the WSGI triggers Django and site loads perfectly. But still all the anchor tags point to http://x.x.x.x/viewFunction/ and not http://x.x.x.x/djangoSite/viewFunction/ and hence I get a 404 every time...
Is there a way to tell Django that the base URL for the site is not the domain (public IP) but instead domain/djangoSite/ from where the WSGI alias starts...
Regards
You don't show the code, but you are probably hard-coding the URLs in your templates. Don't do this: you should always use the {% url %} tag to calculate them dynamically. When you do this, Django will automatically apply the base path (passed as the SCRIPT_NAME parameter in the WSGI environment) and all will be well.
Related
We're hosting several dockerized web-apps on our webserver, let's call it group.example.com. Our subdomains are handled via nginx as suffixes though, which translates to something like group.example.com/app1/ group.example.com/app2/ as root urls.
When using Django, we run into problems though, as all its urls generated by url in the templates such as home will be relative links, so rendered to home. This relative link will not be interpreted correctly, leading to the main, non-app page group.example.com.
So the goal is to have an app based prefix such as /app1/ for all links. I can hardcode these for static links, but there has to be a more elegant way. Also this leads to problem for the used forms submitting to the wrong page - redirecting again back to the main, non-app page group.example.com.
I tried adding /app1/ to all registered urls as prefix, but that doesn't seem to work either - that way the app is running but user would need to visit group.example.com/app1/app1/ to get to the index, and the relative links still don't work correctly.
In the app docker-container we're running the web-app with nginx and uwsgi. It works fine when using correct subdomains such as app1.example2.com - but we don't have that capability on our new faster webserver we want to host the app on.
Is there a way to resolve this using the app containers nginx, uwsgi or django / middleware config to get the links to resolve to group.example.com/app1/ as root?
As far as I know, there is two ways to resolve it.
One use SCRIPT_NAME in the NGINX configuration. For example, based on this server fault answer:
location /app1/ {
SCRIPT_NAME /app1;
# rest of the config
}
Two You can add FORCE_SCRIPT_NAME in your settings.py:
FORCE_SCRIPT_NAME = '/app1'
FYI, I would prefer using first solution.
I am developing a django app and want to use relative a href url paths, as I develop on localhost and will be working on www.example.com. What is the best way to make a relative link like this in my template.html files?
That's ok. You can get relative url using the following in Django template(but for making REST API calls you should use absolute url).
{{ request.path }}
eg. In home page,
Contact
Suppose my server is running on localhost and listening on port 8000 and I visit home page, then the above will be interpreted by django as follows,
Contact
In case of making REST API calls use the following in your template with HOST, PORT and PATH(eg. In home to get all posts),
All posts
And it will be rendered as follows,
All posts
I have one little example as send_contact_data.js. Visit and see line 8.
I'm moving a project to new hosting and would like to set it up such that it sits at mysite.com/test/ (this is under mod_wsgi on an Apache server). This seems to do alright for the application itself, but when I use #login_required to enforce authentication Django redirects to mysite.com/accounts/login instead of mysite.com/test/accounts/login as I would like. I also have a mysite.com/prod that I want to do this same thing on so I don't want to hard code this anywhere in settings... it should figure out where the root of its URL is and act accordingly.
How do I set it up so that Django automagically redirects to what Apache considers that application's web root?
You need to set LOGIN_URL and LOGOUT_URL to full URL path in Django settings file. See:
http://docs.djangoproject.com/en/1.3/ref/settings/#login-url
Django doesn't automatically insert the mount point at the start of those as so have to be fully qualified.
The same problem can be solved in a more generic way for all project URLs. You could checkout an alternative solution at Running a Django site on my local machine, am I redirecting my URLs properly? for an environment based ROOT URL support.
I've got a base url. http://baseurl.com/
I'm trying to run projects on the back of it. For example
http://baseurl.com/mongoose/
The projects run but the URL don't work properly because they all
reference the base url. So for 'About Me' page it points to
http://baseurl.com/about instead of http://baseurl.com/mongoose/about
Is this something i need to change in django or apache? Is what I'm
trying to do even possible?
Coming from an IIS .net background I know that in IIS you can "Create and application" within a site which essentially does what I'm trying to achieve now with Apache and Django.
Thanks
You shouldn't need to do anything. Apache is supposed to be setting a request header called SCRIPT_NAME, which is your base URL, and all URL reversing takes that into account.
How are you creating these URLs in your templates?
Update
So your problem is with getting the URLs of Flatpages. The issue is that the normal way of calculating URLs dynamically, so that they do take SCRIPT_NAME into account - using the reverse() function or the {% url %} tag - doesn't work with Flatpages, because they are not dispatched via urls.py but via a custom middleware which fires on a 404.
So instead of using that middleware, I would use the urls.py mechanism to dispatch to flatpages. Remove the flatpagemiddleware from your settings.py, and in urls.py at the end of your patterns add this:
url(r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage', name='flatpage'),
Now, in your templates, you can do:
<a href="{% url flatpage page.url %}">
and it should work correctly.
Check any urls.py in the project(s) to see if they expect to be top-level. But if the application outputs links like /something then it's going to mean the root directory. The application should be reversing a view/parameter into a URL, which would allow you to move it around. If you wrote the apps, check out reverse in django.core.urlresolvers
Ok I think I must be missing something. In settings.py I don't see the setting for absolute url for the site. I see there is MEDIA_URL.
django-registration references {{ site }} which is defaulting to example.com but I'm not seeing that in any settings.
If I want the dev.mysite.com and mysite.com to work independently, what do I need to put in settings.py?
The site uses the sites framework. The example.com domain is defined in the database and can be changed with the Django admin.
To use 2 sites simultaneously you will have to change the SITE_ID in the settings and add the extra site in the Django Admin.
If you only want to use it from your own code -- just put the domain in a variable in settings-production.py and settings-dev.py (or whatever you choose to call them).
django itself won't do pay any attention to the domain you specify -- but it doesn't need to.