Django Debug Toolbar BaseConnectionHandler.all() error - django

I am using docker and the debug toolbar gives the following error:
BaseConnectionHandler.all() got an unexpected keyword argument 'initialized_only'
I wrote the following code in the settings.py file :
if DEBUG:
MIDDLEWARE += [
'debug_toolbar.middleware.DebugToolbarMiddleware',
]
INSTALLED_APPS += [
'debug_toolbar',
]
import os
import socket
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[: ip.rfind(".")] + ".1" for ip in ips] + ["127.0.0.1", "10.0.2.2"]
I wrote the following code in the urls.py file :
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
path('__debug__/', include(debug_toolbar.urls)),
]

For some reason django-debug-toolbar==3.5.0 broke backward compatability with Django lower than 4.1b1.
In 3.5.0 next changes were added:
https://github.com/jazzband/django-debug-toolbar/commit/4b77ec74f2d326013d715453d7a2219e574c3f6a#diff-72ecd973e54107d746eff0947206cbdbe24cbb3c42216b00615e64d49ca70d73R216-R217
But those changes need this changes in Django 4.1b1 to work:
https://github.com/django/django/commit/4f92cf87b013801810226928ddd20097f6e4fccf#diff-dbe1d4538efcca9f9a6157d5d3de919e0844835a7ccc698bb8c5d4a9eb06e274R75-R81
Fix the version of django-debug-toolbar to 3.4.0 before issue is solved. Opened issue in github:
https://github.com/jazzband/django-debug-toolbar/issues/1645
UPD: Django 3.2.4+ will also work and thats probably better solution.

Related

Static method returning empty list in Django

I am trying to run a Django app on local server. It works fine on mu Ubuntu machine but in mac, I can't get the CSS for
localhost:8000/admin
and
localhost:8000/docs to load.
On digging further, I found out that the static URL in main "urls.py" file
return an empty list instead of a URL pattern.
Does anyone have an idea why it is like that on the new mac system?
I have had the same issue. I was able to fix it manually by adding the following snippet to my urls.py file in the project (the urls.py file next to settings.py)...
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve
. . .
if settings.TESTING_PRODUCTION:
urlpatterns += [
re_path(r'^static/(?P<path>.*)$', serve, {
'document_root': settings.STATIC_ROOT,
}),
]
I pulled this together from the Django Docs here.
I needed to do this so that I could test the "production" environment with manage.py runserver by manually setting DEBUG = False in settings.py which changes a few other URLs and also turns off trace printing in my code.
In my settings.py file, I have some code to set TESTING_PRODUCTION to True as well. But in actual production with a real web server, the code should set TESTING_PRODUCTION to False so that the static files can be served by the webserver directly and not through Django.

URLpattern match doesn't work as expected

What I want
I'm running the very first steps of the Django Project tutorial and there's already something i can't get right.
The idea behind include() is to make it easy to plug-and-play URLs. Since polls are in their own URLconf (polls/urls.py), they can be placed under “/polls/”, or under “/fun_polls/”, or under “/content/polls/”, or any other path root, and the app will still work.
I built everything as needed and my 'shelves' server works fine (debug here, running on localhost).
I'm setting the urlpatterns in shelves.urls, the first of which tries to include 'bluebook.urls'.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('bluebook/', include('bluebook.urls')),
path('admin/', admin.site.urls),
]
When I go to http://127.0.0.1:8000/bluebook/, it works fine and loads the views.index I set up.
What doesn't work
When I go to http://127.0.0.1:8000/thebluebook/, it throws a 404. It goes as far as defining regex match not working :
Using the URLconf defined in shelves.urls, Django tried these URL patterns, in this order:
1. bluebook/
2. admin/
The current path, thebluebook/, didn't match any of these.
It does the same with http://127.0.0.1:8000/the_bluebook/ or http://127.0.0.1:8000/go/bluebook/.
What I tried
I haven't written much code so there's not much to join to this summary. Although the error message only mentions shelves.urls, i checked that bluebook.urls is set to accept any regex after the redirection from shelves.urls :
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I checked Django Tutorial Part 1 Error: URL does not match URL patterns and tried to change ROOT_URLCONF from 'shelves.urls' to 'urls', but it throws a ERR_CONNECTION_REFUSED instead of a 404, whatever I put after http://127.0.0.1:8000/, basically the site doesn't work anymore.
I checked out Page not found 404 on Django site? but my site's urls.py is in the child folder mysite/, not the parent mysite/.
I also tried
urlpatterns = [
path('bluebook/', 'bluebook.urls'),
path('admin/', admin.site.urls),
]
but it doesn't work either.
Basically, all my setup looks right to me as calling the exact URLpattern works as expected. Only the regex matching doesn't seem to work.
FWIW, I'm using JetBrain's PyCharm to edit the code and setup the venv, without the plugin, but I don't see it influencing the running of Django code.
Python-version : Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Django version : 2.0.7
PyCharm version : 2018.1.4
Chrome version : Version 67.0.3396.99 (Official Build) (64-bit)
You didn't include 'thebluebook' in your urls. you have path('bluebook/', include('bluebook.urls')), and path('', views.index, name='index'),
which means you can do http://127.0.0.1:8000/bluebook/any/thing/here/ but you can not http://127.0.0.1:8000/any/thing/here/bluebook/.

django-rest-swagger double slash

I'm having trouble with django-rest-swagger. I did everything (or I think I did) like in swagger documentation, but when I try to test API via "Try it out!" button, it sends request like this, with double slashes
"GET /api//activity/ HTTP/1.1" 404 8388
my_app/urls.py
router = routers.DefaultRouter()
router.register(r'activity', ActivityViewSet)
router.register(r'diary', DiaryViewSet)
router.register(r'discipline', DisciplineViewSet)
router.register(r'ingredient', IngredientViewSet)
router.register(r'product', ProductViewSet)
router.register(r'mealtype', MealTypeViewSet)
router.register(r'meal', MealViewSet)
urlpatterns = [
path('docs/', get_swagger_view(title='API')),
]
urlpatterns += router.urls
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('diet_app.urls'))
]
How it looks at swagger site
What could be the problem?
I ran into the same problem; it is an upstream issue for those of us who use Django 2.0. The core issue was in django rest framework versions 3.7.3 and earlier. Running following command should solves the problem
pip install --upgrade djangorestframework
If you care to see the github history, thank the people who thought through these following two tickets.
See this github ticket https://github.com/encode/django-rest-framework/issues/5686 (closed) and has been consolidated into
https://github.com/encode/django-rest-framework/issues/5675

Cannot get django-debug-toolbar to appear

No matter what I do, I simply cannot get django-debug-toolbar to appear. I've tried everything suggested in every answer on this question.
I have DEBUG=True in my settings
I have django.contrib.staticfiles and debug_toolbar in INSTALLED_APPS
I have 'debug_toolbar.middleware.DebugToolbarMiddleware' high up in MIDDLEWARE_CLASSES
I have INTERNAL_IPS = () in my settings
I tried adding print("IP Address for debug-toolbar: " + request.META['REMOTE_ADDR']) in a view, and it printed IP Address for debug-toolbar: 127.0.0.1
I have a closing </body></html> in my template
I have run pip install django-debug-toolbar in my virtualenv, without any issues
I have run python manage.py collectstatic and there is a debug_toolbar directory in my static files
When I run the app, I see no request in the console for any URLs containing django_debug_toolbar, so I suspect it's the application not being loaded.
I don't see any failed requests in the developer console, either.
I've read the django-debug-toolbar installation docs and am out of ideas.
Does anyone have any suggestions for debugging? I'm running OSX and Django 1.7. The curious thing is that debug-toolbar WAS appearing just fine - I think I've made some tweak that caused it to vanish, but I don't know what.
UPDATE: I've even tried adding this in my settings file, which is supposed to force the toolbar to appear:
def show_toolbar(request):
return True
SHOW_TOOLBAR_CALLBACK = show_toolbar
But it doesn't help.
I've also tried throwing a deliberate exception in my view, so that I can check DEBUG is on and all the settings are as above. They are, and still no toolbar!
UPDATE 2: When I set INTERNAL_IPS=('127.0.0.1',), I start to see debug-toolbar requests in the console, but no toolbar on the page.
And the following HTML appears in my page - so the toolbar is there, but it's not visible because it's got display=none set all over it:
I had the same problem but managed to fix it following dvl's comment on this page. Here is a summary of the fix:
In settings.py
if DEBUG:
MIDDLEWARE += (
'debug_toolbar.middleware.DebugToolbarMiddleware',
)
INSTALLED_APPS += (
'debug_toolbar',
)
INTERNAL_IPS = ('127.0.0.1', )
DEBUG_TOOLBAR_CONFIG = {
'INTERCEPT_REDIRECTS': False,
}
In the project urls.py, add this url pattern to the end:
from django.conf import settings
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
Some information for news users as me, when dev on virtual or remote machine
Add this ligne in a views.py file
print("IP Address for debug-toolbar: " + request.META['REMOTE_ADDR'])
When the views is call, you can see the client IP in the shell
You have to add this IP the settings.py file
INTERNAL_IPS = ('IP')
All of the divs with display: none; are in fact behaving properly. They won't change to display: block; until you actually click on them in the toolbar itself.
The button used to toggle the toolbar is the div with an id="djDebugToolbarHandle". As you can see in your console, this button has a top position of 2310px. What this means is that it is rendering, but it is just way down off the page.
Try typing the following in the console to reset its position:
document.getElementById('djDebugToolbarHandle').style.top="30px";
I had the same problem. Changing the finder module in my settings.py worked for me:
STATICFILES_FINDERS = (
#'django.contrib.staticfiles.finders.FileSystemFinder', #THIS BREAKES debug_toolbar
'django.contrib.staticfiles.finders.AppDirectoriesFinder', #THIS WORKS
)
Make sure to clean the browser cache after this change.
But after this, Django gave me error messages during collectstatic, due to this issue. I solved creating two configurations in my settings.py:
class Production(Base):
DEBUG = False
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
)
class Develop(Base):
DEBUG = True
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
I hope it helps.
One reason why django-debug-toolbar might appear but not appear correctly, (items stuck in "Loading") is if manage.py collectstatic has not been run. Just thought I'd post that here in case it helps someone.
i was have same issue with django-toolbar
all tags have class djdt-hidden and hidden
<div id="djDebug" class="djdt-hidden" dir="ltr" data-default-show="true">
i using pycharm and GoogleChrome
just using FireFox and it was fixed

Django include url causes rendering error?

I have a working django project. I wrote a small app - pm - and I tried to include its urls.py in the active project:
urlpatterns = patterns('',
# ... some urls here
url(r'^$', views.home, name='vw_home'),
# I added the following line:
(r'^pm/', include('pm.urls')),
Once I access the main web page, I receive the following error:
TemplateSyntaxError at /
Caught error while rendering: syntax error
and the debug shows the problem in the following line:
Home
If I remove the last url pattern (the include()), the page renders without any problem.
How can this be fixed?
EDIT:
Adding the urls.py of the pm app:
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns("pm.views",
url(r'^inbox/$', 'inbox', {'folder': 'inbox'}, name='vw_inbox'),
url(r'^sent/$', 'inbox', {'folder': 'sent'}, name='vw_sent'),
url(r'^message/(?<message_id>\w+)/$', 'read_message', name='vw_read_message'),
url(r'^compose/(?P<profile_id>\w+)/$', 'compose_message', name='vw_compose_message'),
url(r'^reply/(?P<message_id>\w+)/$', 'compose_message', name='vw_reply_message'),
)
url(r'^message/(?<message_id>\w+)/$', 'read_message', name='vw_read_message'),
You missed ?P