Finding the site_id of a Site in Django Admin
Asked Answered
O

3

8

I'm installing Django AllAuth into my project and have come across the following line in the documentation for that app (see docs here):

Add a Site for your domain, matching settings.SITE_ID (django.contrib.sites app).

My settings.SITE_ID is 1, unsurprisingly. How do I 'match' this in the Django Admin? I only have 2 fields - Domain Name and Display name.

Conversely, if I create a site in the admin, how do I know what the site_id for it is?

Offshore answered 13/1, 2014 at 5:59 Comment(0)
B
11

You can find the site_id in the address bar.

screenshot of web browser showing Django administration and url of interest

So the line in settings.py would be: SITE_ID = 3

Boswall answered 13/1, 2014 at 6:10 Comment(3)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.Tripalmitin
@Tripalmitin how do I know what the site_id for it is? You can find the site_id in the address bar.Boswall
This certainly helps, thanks - it does indeed answer the 'conversely' question. I'd still be interested to know if there is any way to edit/create the site_id as you are creating a new site (if possible)Offshore
S
4

Go the your project path where you have manage.py file and follow the following steps:

Run teh following command in your terminal

    python manage.py shell

Then in the python shell type:

    from django.contrib.sites.models import Site

    new_site = Site.objects.create(domain='....', name='....')

    print new_site.id

This printed value will be the site_id for site matching query If you are still struck, reply. I will guide you to very simple and easy steps to enjoy the beauty of django allauth.

Scarcity answered 13/1, 2014 at 21:39 Comment(2)
Thanks for the answer - I take it you still can't choose the id - but at least you can find it out?Offshore
@Scarcity I am too having the same problem. I am testing on localhost which is added in Sites and has id of 2 and thus i set SITE_ID = 2 in settings.py but then I get "Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings.." once I click facebook. I have also added localhost as Site Url for my facebook app. What have i done wrong ?Fanning
F
2

This works for me:

user$ python manage.py shell --settings your-settings.py

[ ... banner ... ]
>>>
>>> from django.contrib.sites.models import Site
>>>
>>> sorted([(site.id,site.name) for site in Site.objects.all()])
[(1, u'www.lvh.me'), (2, u'example.com'), (3, u'www.example.com'),...]
>>>
>>> quit()
user$
Francophobe answered 3/10, 2017 at 1:19 Comment(1)
this should be accepted answer, thank you for this, saved me a lot of headacheActinomycete

© 2022 - 2024 — McMap. All rights reserved.