Where do I set my site url in django settings? - django

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.

Related

Issue with Django URLs on apache production server

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.

django-registration - how do i change example.com in the email?

I have the django-registration setup and it sends emails - yay!
However, it's decided that it would be ace to call my site "example.com", which is not the name I decided to use. Cool name, but not for me.
How do I change example.com to something else? I read somewhere that I go to the admin page but - spoiler alert - I've never used the admin page in django and am not actively planning on this currently (but maybe someday?)
So I go to the admin page - /admin/sites/site/ - and sure enough I can see "example.com" and "domain name". But now what? I added my sites domain name and "display name", but how do I select to use it? I even deleted example.com!
However, the email insists on still sending out example.com. All the sites I read just say "use admin", but I don't know if maybe my admin is broken (which would be odd, as I really haven't touched it) or if there is a link i'm missing?
Any ideas?
The entry you saw originally on the admin/sites/site/ page, with example.com and domain name, is the one you should simply edit for the quickest results (instead of adding a new Site object).
What's really happening?
In your settings file, Django automatically defines a SITE_ID property when you create your project, which is by default set to 1. This points to the example.com Site you see in the admin. If you want to use the Site object you created, or change Site objects later when you need the functionality, you can change that SITE_ID property to the id of the Site object you created. Hope this helps!
The site object in the template comes from the Django Site model. When you do a syncdb, it defaults automatically to example.com
If you login to Django's admin interface, you will find "Sites". Inside it, you will be able to change example.com to whatever you like. Just set the id of the site u want to use as current site in your settings.py as SITE_ID = ID.
e.g if you editted the example.com domain name to mysite.com and display name as MYSITE. then SITE_ID = 1 because that is the only content we have in our "Sites" table and its ID is 1. if like to add another "Sites" details leaving the example.com untouched, remember to set the SITE_ID to ID of the one u added

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.

Use Django flatpages without sites

Is there any that I can have a catch all site with flatpage framework in Django?
I have one site but you can get to it through a few DNS names that change constantly. I need to map these all to a single site and flatpages seems bent on me pre-specifying my domain entries.
You need to configure your webserver correctly so that the requests from all domains get forwarded to your only django instance! You cannot run flatpages without having django.contrib.sites in your INSTALLED_APPS, but that is no problem for your case, the actual site will always be determined with the SITE_ID defined in your settings.py. The sites framework does not check the request to check which is the actual site. If you run multiple sites, you have to run multiple django instances that use different settings, which define different SITE_IDs!
So just check your webserver to have everything directed to your django instance!

How to make Django url dispatcher use subdomain?

I have a vague idea on how to solve this, but really need a push :)
I have a Django app running with apache (mod_wsgi). Today urls look like this:
http://site.com/category/A/product/B/
What I would like to do is this:
http://A.site.com/product/B
This means that the url dispatcher some how needs to pick up the value found in the subdomain and understand the context of this instead of only looking at the path. I see two approaches:
Use .htaccess and rewrites so that a.site.com is a rewrite. Not sure if this does the trick since I don't fully understand what the django url dispatcher framework will see in that case?
Understanding how the url dispatcher DO work I could write a filter that looks at valid sub domains and provides this in a rewritten format to the url dispatcher code.
Any hints or solutions are very much appreciated! Thanks.
Have you looked at django.contrib.sites? I think a combination of that, setting SITE_ID in your settings.py, and having one WSGI file per "site" can take care of things.
EDIT: -v set.
django.contrib.sites is meant to let you run multiple sites from the same Django project and database. It adds a table (django.contrib.sites.models.Site) that has domain and name fields. From what I can tell, the name can mean whatever you want it to, but it's usually the English name for the site. The domain is what should show up in the host part of the URL.
SITE_ID is set in settings.py to the id of the site being served. In the initial settings.py file, it is set to 1 (with no comments). You can replace this with whatever code you need to set it to the right value.
The obvious thing to do would be to check an environment variable, and look up that in the name or domain field in the Site table, but I'm not sure that will work from within the settings.py file, since that file sets up the database connection parameters (circular dependency?). So you'll probably have to settle for something like:
SITE_ID = int(os.environ.get('SITE_ID', 1)
Then in your WSGI file, you do something like:
os.environ['SITE_ID'] = 2
and set that last number to the appropriate value. You'll need one WSGI file per site, or maybe there's a way to set SITE_ID from within the Apache setup. Which path to choose depends on the site setup in question.
The sites framework is most powerful where you use Site as the target of a ForeignKey or ManyToManyField so that you can link your model instances (i.e. records) to specific sites.
Mikes solution is correct if you want to have multiple sites with same apps with different content (sites module) on multiple domains or subdomains, but it has a drawback that you need to be running multiple instances of the Django process.
A better solution for the main problem about multiple domains or subdomains is to use a simple middleware that handles incoming requests with the process_request() function and changing the documented urlconf attribute (link) of the request object to the URLconf you want to use.
More details and an example of the per-request or per-domain URL dispatcher can be found at:
http://gw.tnode.com/0483-Django/
Try adding a wildcard subdomain: usually *.