how to load django debug_toolbar dynamically in your settings - django

For a number of reasons, running from unit testing performance to migration issues (Django Migration Error: Column does not exist), I have found it useful to turn the debug toolbar on and off.
Here's a way I've found to control loading it from environment variables.
No, not really a question, think of it as a recipe that I wish I had found on SO.

I believe putting
def show_toolbar(request):
if DEBUG:
return True
DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": show_toolbar,
}
in settings.py is the recommended way and perhaps a bit simpler?

settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
#conditionally disable later on
'debug_toolbar',
#...my apps...
)
#disable if not in DEBUG or if $USE_DEBUG_TOOLBAR is not set.
USE_DEBUG_TOOLBAR = bool(int(os.getenv("USE_DEBUG_TOOLBAR", 0))) and DEBUG
#disable as well if running unit tests...
pgm = os.path.basename(sys.argv[0])
if not USE_DEBUG_TOOLBAR or pgm.startswith("test") or pgm.startswith("nosetests"):
li = [app for app in INSTALLED_APPS if not app == "debug_toolbar"]
INSTALLED_APPS = tuple(li)
and your command line use might look like:
export USE_DEBUG_TOOLBAR=1 && python manage.py runserver

Related

improperly configured app in settings.py of django

Tying to register my djano app in the settings section of my django project. but when i run the server i am getting an error in the terminal.
django.core.exceptions.ImproperlyConfigured: 'site1app.apps' does not contain a class 'Site1appConfig'. Choices are: 'Site1AppConfig'.
I dont know what's going wrong as i just added it to the list of installed apps in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'site1app.apps.Site1appConfig',
]
This looks like a miss on the capitalization for your class
Change:
'site1app.apps.Site1appConfig'
To:
'site1app.apps.Site1AppConfig'

ImportError: No module named threadedcommentsdjango_comments while use django-threadedcomments app

I am trying to use django-threadedcomments app to allow comments in my website following the configurations mentioned https://pypi.python.org/pypi/django-threadedcomments. my installed apps are
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# for comments
'threadedcomments'
'django_comments',
'django.contrib.sites',
# installed apps
# APPS
'debug_toolbar',
#related to debug_toolbar
'haystack_panel',
'whoosh',
'haystack',
'taggit',
'question',
]
COMMENTS_APP = 'threadedcomments'
After adding the 'threadedcomments' app in the settings.py I am gettting the error when I run python manage.py migrate or python manage.py runserver
ImportError: No module named threadedcommentsdjango_comments
Full Error Trace
however the code works fine without 'threadedcomments' and with 'django_comments'.
I am using django 1.8.8 and posgres9.5. please help.
note:
I have already installed 'threadedcomments' app in my venv.
OK, In your settings you are missing ','(comma):
'threadedcomments'
'django_comments',
===>
'threadedcomments',
'django_comments',
If you were accurate you can find that in your own screen:
No module named threadedcommentsdjango_comments
there is no split between two packages.

Django DatabaseError: relation "django_site"

I currently have a django app am developing on my PC with data in my db but when i try running this app on a test server i get the error below
DatabaseError: relation "django_site" does not exist
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...
can any one tell me why am getting this error please.thanks
You may be calling a site object before creating site model(before syncdb)
ex: site = Site.objects.get(id=settings.SITE_ID)
This issue continues to plague many, including myself. Although a tedious process, this approach saves me the brain power:
Disable all external apps in your INSTALLED_APPS, except your own apps, like so:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'main', # This is my own app.
# 'compressor',
# 'ckeditor',
# 'imagekit',
# 'debug_toolbar',
# 'rest_framework',
# 'allauth',
# 'allauth.account',
# 'allauth.socialaccount',
# 'allauth.socialaccount.providers.google',
# 'allauth.socialaccount.providers.facebook',
)
Run
python manage.py makemigrations
python manage.py migrate
Then, uncomment all the other apps, then repeat makemigrations and migrate above.
That works all the time for me
I can't see your models or what apps are you using, but my guess is that you are using django_site (Site model) and you don't have 'django.contrib.sites' in the INSTALLED_APPS.
If I'm correct then just add 'django.contrib.sites', to your INSTALLED_APPS.
Had this strange issue while initiating a new database and using django-debug-toolbar. Removed it from the INSTALLED_APPS and was able to run syncdb. Then re-added debug_toolbar and it was still working fine.
If you're using django-debug-toolbar, try to comment out debug_toolbar in your installed apps and try again.
Update: Please follow the steps for the explicit setup: http://django-debug-toolbar.readthedocs.org/en/1.2.2/installation.html#explicit-setup
i have same problem and fixed it like this:
add SITE_ID=1 into settings.py
run this command :
python manage.py migrate
not been able to solve this a django way so i tried using sql, i created a dump of just the database like this.
pg_dump mypgdatabase | gzip -c > mypgdatabase.dump.out.gz
then moved it to the server
scp /path/to/mypgdatabase.dump.out.gz my_remote_server
then recreated it on the server like this
psql -d mypgdatabase -f mypgdatabase.dump.out
then run
./manange.py migrate --all
and all when well.
Couple of things you can try and check:
Your settings.py should have a SITE_ID usually = 1
Change order of your INSTALLED_APPS in your settings.py and try temporarily commenting items out.
As Geo said, check that you're not calling a site object before creating site model (ex: site = Site.objects.get(id=settings.SITE_ID)).
Once you got it working, remember to manage.py makemigrations APP_NAME as I found cases where it seem to have then avoided the commenting out step.
I overcame this issue with the following order in INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.sites',
'allauth',
'allauth.account',
# my other apps,
)
My "commenting out" pattern was slightly different than the accepted answers (as shown below). That is to say, it might be unique depending on the values of various dependencies scattered throughout your app. If commenting out the values above throws an error message, I recommend commenting out the apps that throw the error message and uncommenting accordingly until it works for you. This may be a little "brute force" but it should get the job done.
INSTALLED_APPS = [
# django native apps
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
"django.forms",
'django.contrib.gis',
# 'django.contrib.flatpages',
#third party apps
#django-allauth apps
'allauth',
# 'allauth.account',
# 'allauth.socialaccount',
# 'allauth.socialaccount.providers.facebook',
# 'allauth.socialaccount.providers.google',
# 'allauth.socialaccount.providers.twitter',
# 'allauth.socialaccount.providers.github',
# my apps
'app0',
'app1',]
#MIGRATION_MODULES = {"sites": "mysite.contrib.sites.migrations"}
Also make sure SITE_ID = 1 comes before the DATABASE definition in settings.py

django gravatar not getting picked up as a module - what am i doing wrong?

so here is my problem - the gravatar app (from google projects - here! http://code.google.com/p/django-gravatar/) in my django project crashed everything on startup.
i get this error Error: No module named gravatar
this is my installed apps:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'dumpstown.dumpstownapp',
'bootstrap_toolkit',
'registration',
'gravatar'
)
and from the console when i run this:
>> import sys
>> print sys.path
['', 'C:\\development\\python\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg',
'C:\\development\\python\\lib\\site-packages\\pip-1.1-py2.7.egg', 'C:\\developm
ent\\PycharmProjects\\lib\\gravatar', 'C:\\Windows\\system32\\python27.zip', 'C:
\\development\\python\\DLLs', 'C:\\development\\python\\lib', 'C:\\development\\
python\\lib\\plat-win', 'C:\\development\\python\\lib\\lib-tk', 'C:\\development
\\python', 'C:\\development\\python\\lib\\site-packages']
what am i missing here?
UPDATE
huh, weird. I've checked the gravatar folder, and i have
templatetags dir
__init__.py
models.py
views.py
also, if i change the installed apps section to read:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'dumpstown.dumpstownapp',
'bootstrap_toolkit',
'registration',
'templatetags'
)
then the server starts up fine and runs. But i... i'm all confused now! Am i meant to import the templatetags? or something else? how do i refer to the gravatar stuff if i'm importing templatetags??
EDIT: Solved.
I just don't understand how python works, it would seem! I was totally adding the wrong item to the pythonpath - i was adding C:\\development\\PycharmProjects\\lib\\gravatar, where i should have really been adding C:\\development\\PycharmProjects\\lib
this solved the problem, and now i can use gravatar as i want. The settings.py entry is as above in my first example (that is, just 'gravatar') and the way i use it is i just {% load gravatar %}
hurrah!
Possibly you checked out the trunk folder and have a structure like ..\gravatar\gravatar? The setup.py script should install it somewhere your path but you could also try changing C:\developm ent\PycharmProjects\lib\gravatar to C:\developm ent\PycharmProjects\lib\gravatar\gravatar
Totally adding the wrong item to the pythonpath - i was adding C:\development\PycharmProjects\lib\gravatar, where i should have really been adding C:\development\PycharmProjects\lib

Help with "Error: No module named polls" from the Django Project Tutorial 1

I am working on this Django tutorial and am getting this error: "Error: No module named polls" when I type "python manage.py sql polls" in the terminal. I have no clue how to fix this problem. Any help would be greatly appreciated.
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',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'polls' # THIS IS THE ANSWER
)
There is an error in the documentation. Type polls instead of mysite.polls.
Ismael's answer worked for me.
Originally had
urlpatterns = patterns('',
(r'^polls/$ ,'mysite.polls.views.index'),
)
Changed to
urlpatterns = patterns('',
(r'^polls/$ ,'polls.views.index'),
)
If you have added polls app inside the setting.py please remove that and try recreating the polls and then add to the setting.py file. This solved my issue :)
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#Remove the below application and try again
#'polls',
)
It might be possible that you missing "," after 'polls.apps.PollsConfig' in settings.py file
This need to run one command after your changes in INSTALLED_APPS
$ python manage.py makemigrations polls
after that:
$ python manage.py sqlmigrate polls 0001
You must create all these files in pool directory: init.py, admin.py, models.py, tests.py,views.py.
succes
Be sure you actually stayed true to the tutorial and named your app "polls", otherwise things won't link up.
Changing from 'polls.app.PollsConfig' to 'polls' worked for me