Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - get_current_site(request) just grabs example.com

My django project is working with a third-party forum. The activation_email is being send through get_current_site(request). The password rest is done by a django built called password_reset from django.contrib.auth.views import.

For another app I needed to install django.contrib.sites. Through that get_current_site(request) does not give me the current site, but "example.com" from django.contrib.sites.models. The problem is:

  • the activation mails does have the wrong domain (example.com)
  • when I change the activation_mail sender in the code, the passwort reset mail still comes with "example.com" because it is using the django built_in way.

My project is online already. So I thought I can delete example.com and add my domain-name. But now I can not sign in anymore and this error occurs: django.contrib.sites.models.DoesNotExist: Site matching query does not exist.

like image 846
pillow_willow Avatar asked Mar 09 '16 17:03

pillow_willow


Video Answer


1 Answers

Your Django project looks for a entry in the Site model but can't find it. Therefore Django raises an DoesNotExist error.

What you can do to untwist this is recreate the deleted Site object. It doesn't need to be called example.com, use the appropriate domain name.

If you locked yourself out. You can always create a shell session. SSH to your server and in the folder with manage.py type:

   $ python manage.py shell
   >>> from django.contrib.sites.models import Site
   >>> site = Site(domain="yourdomain.com", name="My awesome site")
   >>> site.save()

Other model entries can have references to the old site object. Normally the primary key (id) of the first Site object is 1. But your newly created site object has some other id. Most of the time you do not want to change id's because it will break relations. But we try to restore broken relations so here we go:

   >>> print(site.id)  
   2  # The id of the current site object.
   >>> site.id = 1  
   >>> site.save()  
   >>> print(site.id)  
   1  

Or just open a db session and create this object with SQL statements.

like image 156
allcaps Avatar answered Nov 15 '22 12:11

allcaps