How do I deploy Django to a suburl using mod wsgi? - django

All of my URLs in Django are coded relative to the root URL: /login/, /logout etc. I want to deploy the app to the sub url : www.example.com/app, and I want the URLs to be www.example.com/app/login/, www.example.com/app/logout. How do I do this efficiently?

You must modify your hard coded links to use Django's reversal methods. For example, instead of
Login
you should use
Login
this will allow Django to dynamically determine the URLs with respect to the root URL.

Related

Using Browser Router in react-router-dom with Django backend

I'm building an application that uses React as front-end and Django Rest as back-end.
From front-end, I'm using react-router-dom for declarative routing and Django only serves API
So when user enters an URL like this https://myapp.com/something/something-else, it's actually handle by Django and it will return an error since Django doesn't know which page to lead to with this URL. It should be handled by React instead.
One of the workaround I have is to use a Hash Router, so the URL will look like this (with the hash symbol): https://myapp.com/#/something/something-else
But there have been cases where user will just key the URL without the hash sign (as they didn't know).
Is there away to handle this without using Hash Router?
I can't say exactly because you haven't provided code, or an explanation of your project structure.
How are you doing a hash-router? If you have Django serving a single HTML file, then you should be able to edit your urls.py to pass any URLs that don't match the api to the same page.
As an example, I have a Django website with a Preact frontend, and I have Preact files that are built into static .js files, which are then served by Apache. Django does all URL routing and serves HTML files, which then request the Preact JS files from Apache.

django using provider url patch

I am developing a django site that ,on my development environment, is accessed by the url localhost:8000.
The url is automatically redirect to the url localhost:8000/accounts/login and, after login, to localhost:8000/iform/list.
After deployed on my webserver, the app now is called icontrol, so, on the configuration panel of my provider, I set it to respond to the url www.mydomain.com/icontrol.
When using the same files for development and deploy, after the login, instead redirect to www.mydomain.com/icontrol/iform/list, for example, its trying to redirecting to to www.mydomain.com/iform/list.
How to make it work correctly on both environments?
There are a few ways it can be done. The easiest, and most correct way for what you are doing, is to use the LOGIN_REDIRECT_URL setting.
LOGIN_REDIRECT_URL = '/icontrol/iform/list'
If you want more fine control over it, you can probably override the get_success_url like they have done here.
Since you seem to want to use a static URL, I would suggest the first way though.

How can django dispatch visits to views based on domain

For example, I have a django project, there are two pages.
http://domain.com/A/things
http://domain.com/B/things
Now I want that visitors can visits these pages with url
http://A.domain.com/things
http://B.domain.com/things
I tried to use nginx to rewrite the rule. Such as rewrite "http://A.domain.com/things" to "http://domain.com/A/things".
But when I use the function reverse or the templatetag url, the url will still be "/A/things" instead of "http://A.domain.com/things"
As we know, django dispatch visits to views based on urls. I wonder if there is a way to make django dispatch visits based on domain?
Look at django-hosts app. It provides very convenient work with subdomains.
https://github.com/ennio/django-hosts
You could have different Django projects in differente VirtualHosts, another option would be to use the Sites Framework

Hosting Django Project at /test #login_required redirects to /accounts instead of /test/accounts

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.

Apache | Django: How to run websites on the back of a base URL?

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