I am trying to get django-debug-toolbar working. I have followed the installation steps and I get the sidebar including stats (e.g. SQL 1 query in 2.75ms, Static Files 19 Files used, 30 receivers of 12 signals) which seem to be legitimate and indicate that its working.
However, when I click for more info on a given tab, I get a 404 in browser, and this sort of thing in the console:
"GET /__debug__/render_panel/?store_id=ac74875cfe864b2dab4c6d17c1d1ed5d&panel_id=RequestPanel HTTP/1.1" 404 1791"
Other pages on site do work.
I have tried various configurations in urls.py. Here is what I currently have:
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailcore import urls as wagtail_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from search import views as search_views
urlpatterns = [
url(r'^django-admin/', include(admin.site.urls)),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
url(r'', include(wagtail_urls)),
# Alternatively, if you want Wagtail pages to be served from a subpath
# of your site, rather than the site root:
# url(r'^pages/', include(wagtail_urls)),
]
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Serve static and media files from development server
# urlpatterns = [
# url(r'^__debug__/', include(debug_toolbar.urls)),
# ]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have tried a few other configurations here, including having:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
in the if settings.DEBUG: block.
Any ideas?
As the comment says, the wagtail urls must be the last pattern.
One option would be to move the debug toolbar urls to the beginning of the list:
urlpatterns = [
...
]
if settings.DEBUG:
urlpatterns = [
url(r'^__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
Or you could remove the wagtail urls from there current position, and add them after your if settings.DEBUG: block.
if settings.DEBUG:
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
urlpatterns += [
url(r'', include(wagtail_urls)),
]
I’ve included the debug toolbar urls inside and if settings.DEBUG: block here because that’s what the docs recommend, but that’s not the reason why it works. The key is to make sure that the wagtail urls come at the very end.
Related
I am developing a website with Django, I had a lot of pictures in my project, uploaded from the admin panel and saved in the Media folder which I created for these uploads separately, It was working fine and exact way I wanted in months, Suddenly they are just not loading, getting 404 for all of them, without any change in project, they are just not loading.
My media path in Settings.py :
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
I have added this to the end of my urls.py of the app:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and as i said, it was working fine for a long, suddenly this happened
edit: I just figured it out that this is happening when I am using redirect function in one of my views
Another way to do it is:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# other URLs ...
]
# If DEBUG=True in the settings file
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
So we're adding static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to the urlpatterns if DEBUG=True in the settings.py file. This gives a better management in some sense.
I had the same issue and this helps me :
Add this in your urls.py in the urlpatterns list :
from django.views.static import serve
from django.conf import settings
from django.conf.urls import url
urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
# YOUR URLS ARE HERE
]
Of course stay in Debug=True in settings.py.
I got a 404 error on my website when accessing the correct route, did I do something wrong
urls.py handles the main route
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
urls.py handles routes in the apifacebooks app
from django.urls import path
from . import views
urlpatterns = [
path('api/facebook/like-post',views.like_post)
]
And when I go to http://localhost:8000/api/facebook/like-post I get a 404 error
Image error 404
My question has been solved, thanks
In your apifacebooks app change the path because you had "api..." double in the path
from django.urls import path
from . import views
urlpatterns = [
path('like-post/',views.like_post)
]
And in the root urls.py just
path('api/facebook/', ....)
In your code, the url pattern would be "http://localhost:8000/api/facebook/api/facebook/like-post".
As explained id Django docs:
Whenever Django encounters include(), it chops off whatever part of
the URL matched up to that point and sends the remaining string to the
included URLconf for further processing.
Remove "api/facebook/" in "apifacebooks.urls", for example:
Main urls.py
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)/', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
apifacebooks.urls
from django.urls import path
from . import views
urlpatterns = [
path('like-post',views.like_post)
]
Or you can try to place remove "r'api/facebook/(.+?)'", for example:
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
This is my url.py in WebFetcher
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('Fetcher.urls')),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is my url.py in Fetcher
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name = 'home'),
path('page_objects/', views.page_objects, name = 'page_objects')
]
this is my form
<form action="{% url 'page_objects' %}" method="post" enctype="multipart/form-data">
This is the name of my function in views
def page_objects(request):
I am getting 404 error saying
Using the URLconf defined in WebFetcher.urls, Django tried these URL patterns, in this order:
[name='home']
page_ojects [name='page_objects']
admin/
^media/(?P.*)$
The current path, WebFetcher/page_ojects, didn't match any of these.
I ready all the documentation on the URL Dispatcher and I could not find anything that looks wrong with my code. I hope it is just a syntax error. If you think more of my code will be helpful, comment and I will edit this post.
Edit 1:
I updated my WebFetcher urls.py and my Fetcher urls.py per Daniel's suggest.
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from Fetcher import views
urlpatterns = [
path('WebFetcher/', include('Fetcher.urls')),
path('', views.home, name = 'home'),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import path
from . import views
urlpatterns = [
path('page_objects/', views.page_objects, name = 'page_objects')
]
Now the 404 error I am getting is
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/WebFetcher/WebFetcher/WebFetcher/page_objects/
Using the URLconf defined in WebFetcher.urls, Django tried these URL patterns, in this order:
WebFetcher/ page_objects/ [name='page_objects']
[name='home']
admin/
^media/(?P.*)$
The current path, WebFetcher/WebFetcher/page_objects/, didn't match any of these.
You need to go to http://mywebsite/page_objects instead of http://mywebsite/WebFetcher/page_objects.
If you want to have the page_objects url nested under WebFetcher then you can do this to your urls.py:
MyProject/urls.py:
urlpatterns = [
path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
path('admin/', admin.site.urls),
]
Fetcher/urls.py:
urlpatterns = [
path('', views.home, name = 'home'),
path('page_objects/', views.page_objects, name = 'page_objects')
]
Note: your home page will now be at http://mywebsite/WebFetcher - if you want it to be at the root, i.e. http://mywebsite then you can do this instead:
MyProject/urls.py:
urlpatterns = [
path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
path('', views.home, name = 'home'), # move the home page path to the root urls.py
path('admin/', admin.site.urls),
]
Fetcher/urls.py:
urlpatterns = [
path('page_objects/', views.page_objects, name = 'page_objects')
]
You have a typo in your urls.py path.
page_ojects should be page_objects
please write this pattern that will work fine.
path('page_ojects/', views.page_objects, name = 'page_objects')
Updated: add this code to your project level urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('Fetcher.urls')),
]
And if it doesn't solve your issue, then there might a issue in your views.py module, maybe your views doesn't find any correct views to dispatch. so write below code for testing purpose,in yours views.py
from django.shortcuts import render
def page_objects(request):
return render("Hello, pages_objects!!")
I've read the documentation of the Django about serving static files, and it says you should use django.conf.urls.static.static function in order to server them.
Here is my code:
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from settings import STATIC_ROOT, STATIC_URL
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^api/v1/', include('news.rest_urls', namespace='rest_framework')),
static(STATIC_URL, STATIC_ROOT)
]
And this is the error I receive:
'list' object has no attribute 'regex'
You need to define your STATIC_URL and STATIC_ROOT in settings.py and add to your urlpatterns next:
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I have created Djnago project in eclipse. Unfortunately, i am facing issue when i run the project
ImportError at /
No module named urls
Here Error Page
http://dpaste.com/1499981/
Eclipse Project http://i1008.photobucket.com/albums/af204/shoaibshah01/Untitled_zps84f95b4f.jpg
urls.py Content
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'TestApp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Try converting admin.site.urls to string url(r'^admin/', include('admin.site.urls'))
Most likely this is because your TestApp is not referenced in INSTALLED_APPS so Django have no idea it should process it.
Try to change your ROOT_URLCONF to 'urls' value in settings.py
ROOT_URLCONF = 'urls'
The link you provided for the error log is giving 404.
Perhaps you can try with the below code for admin in URLs.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Everything is looking correct in settings.py.
If the above changes are not working, please share error page again.