Django admin DoesNotExist at /admin/ - django

I have some problems with Django admin.
after syncdb, the result is:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.
What does this mean?
Anyway, when I visit the website admin panel http://www.example.com/admin/, I receive this message:
DoesNotExist at /admin/
Site matching query does not exist.
setting.py contains:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
)
ur.py contains:
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'rshd.views.home', name='home'),
# url(r'^rshd/', include('rshd.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)

You don't really need the sites framework if you only run one site from the project, so the easiest fix would be to remove the following item from your INSTALLED_APPS and the error should go away:
'django.contrib.sites'
You can also re-create the missing Site object from shell. Run python manage.py shell and then:
from django.contrib.sites.models import Site
Site.objects.create(pk=1, domain='www.example.com', name='example.com')

You can fix this error if you are using django.contrib.sites without removing it by changing SITE_ID = 1 value in settings.py file.
It is happen to me when I changed domain name on my server, I deleted old domain name from http://mynewdomainname.com/admin/sites/site/ manually, and the old domain record in database has id = 1, also I added new domain name mynewdomainname.com and it is id was id = 2,
I just changed to SITE_ID = 2 and the error gone SITE_ID refers to current "pk" of active domain name you use as default.
In code:
>>> from django.contrib.sites.models import Site
>>> # get_current() came from SiteManager class manager,
>>> # see site-packages/django/contrib/sites/models.py
>>> current_site = Site.objects.get_current()
>>> print(current_site.pk)
2
>>> print(current_site.domain)
'http://mynewdomainname.com'
this happen after settings.py changed
SITE_ID = 2
Be careful about current domain id in django sites app.

The same problem also suddenly came to me, and you know there're many solutions. However, what's the reason of this sudden problem?
After dig in deeply, I found the reason is that, we ignored a step in the first syncdb action.
When you have your first syncdb, django will ask you to create a default account, if you don't input in this interactive show, that site object would not be created automatically.
So be careful of this. I'm using django 1.3.1, don't know whether the latest version has resolved this issue.

Since the above comments are pretty old and the syncdb command doesn't exist, and I don't want to remove django.contrib.sites, here's what worked for me:
Increment SITE_ID. I had SITE_ID = 1, changed it to SITE_ID = 2 and everything worked again.

Add SITE_ID = 1 in your settings.py

just fixed the issue by an another way:
I use PostgreSQL and Django 1.45...
as i removed the www.example.com Site and added a new www.xxx.com Site it was added as ID=2
'cause PostgreSQL doesn't go back in the ID numbers and the login and logout Django-Sites are somehow bound only to the ID=1 Site in your DB...
I went to my PostgreSQL DB and changed the ID of www.xxx.com to 1 and then I was able to see the login and logout Site again :)
btw. [yes, you just can remove the django.contrib.sites from your settings.py if you don't need it ^^ (but I haven't tried this one out in my case, with the ID number problem)]
hope it will work for further users! ;)

Yes change the SITE_IT=1 to SITE_ID=2 and everything is ok

Yes, the solution mentioned at the top can be (part of) the solution.
I've noticed however, that not only including django.contrib.sites without configuration can cause that problem, also including any site of
allauth (pip install django-allauth) might cause this problem if said package is not configured correctly.
(And allauth is as far as I've seen not configured correctly by default...)

Another thing you can do is simple if you get some error or just want to change site.
Download sqlitebrowser
open your sqlite.db
search for django_sites
change domain name and display name to whatever you want

Check your Window task manager and make sure that there is 1 process name 'python.exe' is running. If there are many, delete all of them then restart the server. This solution works for me.

Related

django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: thumbnail

I am developing a django web project that uses the following packages/applications:
sorl-thumbnail
django-oscar
Here is a snippet of my settings.py file:
INSTALLED_APPS = [
'registration', #should be immediately above 'django.contrib.auth'
'django.contrib.auth',
# ...
'zinnia',
'zinnia_tinymce',
'sorl.thumbnail',
'embed_video',
# ...
'django.contrib.flatpages',
'compressor',
'widget_tweaks',
] + get_core_apps()
When I comment out sorl.thumbnail, I am able to run the development server using manage.py runserver. However, if I uncomment the sorl.thumbnail line and try to run the development server, it throws an exception:
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: thumbnail
Now, I am aware that a similar question exists on this site, however, following the instructions in the accepted solution, i.e.:
create a sol_thumbnail folder in same directory as the manage.py script
create sorl_thumbnail/apps.py (see below)
modify myproject/mysite/___init____.py (see below)
sorl-thumbnail/apps.py
from django.apps import AppConfig
class SorlthumbnailConfig(AppConfig):
name = 'sorl-thumbnail'
label = 'sorl.thumbnail'
myproject/mysite/_init _.py
default_app_config = 'sorl-thumbnail.apps.SorlthumbnailConfig'
Why is the fix above not working, and how do I resolve this issue?
BTW: I am using django-1.10
I went though the same problem of duplicated applications, and following exactly the similar question I solved my problem.
The problem with your solution is that you have added default_app_config = 'sorl-thumbnail.apps.SorlthumbnailConfig' to myproject/mysite/___init____.py, but you should have added to myproject/sorl-thumbnail/___init____.py.

RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

I am building an application with Django Rest Framework and AngularJs. I am using Django-rest-auth for my authentication purposes, although, I have not been able to set it up. Anyway, I am trying to set up this app with my project. I realized I need to install django-rest-auth-registration to get it running, so I followed this documentation to do the following things:
I ran the commands
pip install django-rest-auth
and
pip install django-allauth
Any my settings.py looks like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 3rd party apps
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
# My app
'myapp',
]
I have also added the authentication backends, context_processors, and the proper urls.
However, when I try to migrate, my terminal throws the following error:
RuntimeError: Model class django.contrib.sites.models.Site doesn't
declare an explicit app_label and isn't in an application in
INSTALLED_APPS.
Why do I get this error, and how do I solve it to migrate my project? Thanks!
The fix
Just add Django's Sites framework to your apps and set SITE_ID to 1 in your settings.
INSTALLED_APPS = [
...
'django.contrib.sites',
]
SITE_ID = 1
Why does this happen?
Django's Sites Framework is a contributed module bundled with the core library that allows for the use of a single Django application/codebase with different sites (that can use different databases, logic in views, etc). The SITE_ID setting, as stated in the docs, "is used so that application data can hook into specific sites and a single database can manage content for multiple sites."
In this particular case AllAuth requires the Sites Framework in order to function properly. Many other third-party libraries are built to safely handle cases where multiple sites may be present and as such may be best .
I landed on this post via Google search. My problem was running tests that blew up with the error:
RuntimeError: Model class app.taxonomy.models.Term doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
This was running on Python 2.7.x with absolute imports. As mentioned by Colton Hicks in the comments, below, this can also happen with Python 3 (pytest 3.2.3 with Django 1.11.4).
In my tests.py:
from __future__ import absolute_import
[...]
from .models import Demographics, Term
After changing the relative import to an absolute import the problem went away:
from taxonomy.models import Demographics, Term
HTH
Try adding the app_label = 'yourApp' in the models Meta class:
class Meta:
app_label = 'yourApp'
I got the error above. However my problem was the in the urls.py. I was following PyDanny cookiecutter django recipe. My error was to put in the urls.py this line:
url(r'^demo/', include('project.demoapp.urls', namespace='demoapp')),
when I corrected to this:
url(r'^demo/', include('demoapp.urls', namespace='demoapp')),
all was well. I also changed my local apps (I did this first and so the critical error was the url misconfiguration):
LOCAL_APPS = [
# Your stuff: custom apps go here
'demoapp.apps.DemoAppConfig',
]
I have django debug toolbar installed and this was actually causing the/my problem.
INSTALLED_APPS (in settings.py) needs the entry 'django.contrib.sessions'. Make sure to run migrate after adding.
Just add 'django.contrib.sites', to INSTALLED_APPS and set SITE_ID = 1 in your settings.py file.
Upgraded Answer for Django>=4.0 // 2022
Add Django's Sites framework and FlatPages Framework to your INSTALLED_APPS and set SITE_ID in your settings.
INSTALLED_APPS = [
...
'django.contrib.sites',
'django.contrib.flatpages',
]
SITE_ID = 1
Your tests should work like a charm
This error occurred because I had created a new app folder for a subset of sites related to another feature. This needed to be added to my INSTALLED_APPS in settings.py
Django 4.1+ (2023)
After almost an hour digging, what solved for me was this:
INSTALLED_APPS = [
...
'django.contrib.sessions',
]
No need for SITE_ID or additional INSTALLED_APPS entries.
Everything worked as expected after I made a migration
python manage.py migrate
Good luck

Why is the Django-Simple-Captcha image not showing up?

I'm trying to add a Django-Simple-Captcha image to my application's login screen.
This is what I have added at the top of my forms.py file:
from captcha.fields import CaptchaField
This is what I have added to the registration form:
captcha = CaptchaField(
label="What does this say?",
required=True,
)
This is what I added to my site's url.py file:
urlpatterns += patterns(
'',
url(r'^captcha/', include('captcha.urls')),
)
I have also added "captcha" to my INSTALLED_APPS in settings.py
However, when I load the page, I see that the Captcha image is a broken link: http://predictstat.com/accounts/register/. The server shows this on the console:
[23/Dec/2013 16:30:47] "GET /captcha/image/56edd656ba57a2a3e71571373e1a59c564e3d592/ HTTP/1.1" 500 72336
However, there is no such directory "captcha" under the directory for my application. So where is it trying to look for this image? And why doesn't it exist?
On closer inspection, I found the issue was this:
Exception Value: The _imagingft C module is not installed.
Details how to solve this issue can be found here: Python: The _imagingft C module is not installed
There is no need to create any captcha directories.
The problem is that you did not update your urls.py as mensioned in the documentation:
urlpatterns += patterns('',
url(r'^captcha/', include('captcha.urls')),
)
Another problem could be that you did not run syncdb:
./manage.py syncdb
./manage.py migrate # If you use migrations
The same was happening in my application and I manage to make a very strange workaround (XGH alert) usising two slashes in django-simple-captcha 0.4.4
urlpatterns += patterns('',
url(r'^captcha//', include('captcha.urls')),
)
I think it may has something with the conflict of globally and virtureenv environment,may be there is another app which contains the name captcha.

Why does removing 'django.contrib.sites' from INSTALLED_APPS fix the 'DoesNotExist at /admin/' error that I suddenly got?

Yesterday I set up a new Django app and the admin was still ok when I stopped working. This morning I run the server, and I see this error page:
DoesNotExist at /admin/
Site matching query does not exist.
I hadn't touched any of the code or made any changes to the database. While looking for an answer, I saw this post, and indeed, removing 'django.contrib.sites' from INSTALLED_APPS in my settings.py file does "solve" the problem, but now I'm wondering why. I obviously had that line in INSTALLED_APPS yesterday while I was still working, but I'm wondering why it suddenly stopped working today when I hadn't touched any code since then.
You need a Site object to be present in the sites table. For some reason, the Site object was not created.
Try this
Log into your django shell
$> ./manage.py shell
>>> from django.contrib.sites.models import Site
>>> site = Site()
>>> site.domain = 'example.com'
>>> site.name = 'example.com'
>>> site.save()
OR
$> ./manage.py shell
>>> from django.contrib.sites.models import Site
>>> site = Site.objects.create(domain='example.com', name='example.com')
>>> site.save()
This should fix your problem.
You could also refer to this documentation about enabling the Sites framework:
To enable the sites framework, follow these steps:
Add 'django.contrib.sites' to your INSTALLED_APPS setting.
Define a SITE_ID setting:
SITE_ID = 1
Run migrate.
django.contrib.sites registers a post_migrate signal handler which creates a default site named example.com with the domain example.com. This site will also be created after Django creates the test database. To set the correct name and domain for your project, you can use an initial data fixture.

Registered models do not show up in admin

I added a model to admin via admin.site.register, and it does not show up in admin. Since admin is so "It just works", I have no idea of how to debug this. Pointers?
After adding and registering your admin:
# app/admin.py
class YourModelAdmin(admin.ModelAdmin):
pass
admin.site.register(YourModel, YourModelAdmin)
Make sure your app is in your project settings.py:
# settings.py
INSTALLED_APPS = (
# other apps ...
'app',
)
Sync your project for that model if you have not done so already:
python manage.py syncdb
Restart your server, CTRL-C:
python manage.py runserver
In such a situation is also a good practice to check if the user logged in to the admin panel has rights to manage such a model. If they do then you could change your code to access the functions as root.
When in doubt, shut down server, syncdb, start server.
I have the experience, that sometimes after changing an admin.py the dev-sever won't be restarted. in that case touch settings.py helps.
I think the checklist in Thierry's answer is almost definitive, but make sure that urls.py contains admin.autodiscover() to load INSTALLED_APPS admin.py modules.
# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
('^admin/', include(admin.site.urls)),
)
More info in the django docs.
Have you added the application to your installed apps? That has happened to me both one and two times. :) Otherwise it would be useful for us to see the code to help you.
Also make sure there are no syntax errors in your admin.py or anything. That can cause an app to fail to be registered with the AdminSite.
I've faced the same problem, but it was a little tricky than yours.
Consider, that you have a project with, say, five or even more apps. For me it is more obvious to register all models in just one admin.py file, so I have decided to do it in one place - core directory. Of course, it was not an app, so none of models showed up on admin page.
comment out the some lines in urls.py see docs for more details
admin.autodiscover()
urlpatterns = patterns('',
('^admin/', include(admin.site.urls)),
)