2

I want to get the server url in django. So I went through stackoverflow and found out that in order to do this I will have to do the following:

>>> from django.contrib.sites.models import Site
>>> mysite = Site.objects.get_current()

I tried the above inside python manage.py shell in the production server and expected mysite to give me the production server's url but it gives example.com

>>> mysite
<Site: example.com>

Am I missing some configuration or something?

meshy
  • 8,470
  • 9
  • 51
  • 73
Indradhanush Gupta
  • 4,067
  • 10
  • 44
  • 60

2 Answers2

9

The Site object is stored in the database. Change it there, and you're done.

from django.contrib.sites.models import Site
mysite = Site.objects.get_current()
mysite.domain = 'mysite.com'
mysite.name = 'My Site'
mysite.save()

Alternatively, you can change it in the /admin/ section of your site at /admin/sites/site/1/.

This allows you to run multiple sites from the same code-base.

meshy
  • 8,470
  • 9
  • 51
  • 73
2

You could try build_absolute_uri - see How can I get the full/absolute URL (with domain) in Django?

Community
  • 1
  • 1
donatello
  • 5,727
  • 6
  • 32
  • 56