How to disable django REST framework docs page - django

I am currently using Django REST framework and it works very well for me. The problem is that the API documentation page is public in production and I would like it to only be seen in development (localhost) or by administrators users.
This is my urls.py:
schema_view = get_schema_view(title='My API')
router = DefaultRouter()
# router.register(...)
urlpatterns = [
url(r'', include(router.urls)),
url(r'^docs/', include_docs_urls(title='My API service')),
url(r'^schema/$', schema_view),
]
PS: I'm using the version 3.9.2

You can always check for the DEBUG value and do stuff if it is set (which is set to True in development mode). For example:
from django.conf import settings
urlpatterns = [
url(r'', include(router.urls)),
url(r'^schema/$', schema_view),
]
if settings.DEBUG:
urlpatterns += [
url(r'^docs/', include_docs_urls(title='My API service'))
]

Related

How to set up a homepage at django

I am new to django and I want to set my homepage url
My homepage is 127.0.0.1:8000/home but I want my homepage at 127.0.0.1:8000/ or I want my django start as 127.0.0.1:8000/home as default. How do I do it?
Use empty string in url path instead 'home/' for root url
For example:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app_name.urls'))
]
for urls in your app
urlpatterns = [
path('', views.home, name="home"))
]

How to specify basehref in Django

Is it possible to launch Django app with BASEPATH=/api or just BASEPATH=/ based on any configuration?
Based on #narenda-choudhary comment
In file settings.py, where BASEPATH is env variable with relative path
BASEHREF = os.environ.get('BASEPATH', '/')
Then in urls.py
urlpatterns = [
url(r'^{}'.format(settings.BASEHREF), include([
path('admin/', admin.site.urls),
])),
]

How to Internally redirect from home page to Catalogue page in Django-oscar?

I'm using Django-oscar for my project. After Django-oscar config, I can see localhost:8000 empty, Instead, I want to redirect to localhost:8000/Catalogue Page
urls.py
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', admin.site.urls),
url(r'', application.urls),
]
I expected output is. when i run localhost:8000 it should take to localhost:8000/catalogue

Django debug toolbar 404 with wagtail

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.

Django REST Framework URLs with Django 2.0

I'm trying to set up Django REST Framework with Django 2.0 project which means url(r'^something/' ... has been replaced with path(something/ ....
I'm trying to work out how to set up my rest_framework patterns.
This is what I have:
router = routers.DefaultRouter()
router.register(r'regulations', api.RegulationViewSet)
router.register(r'languages', api.LanguageViewSet)
urlpatterns = [
...
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
...
]
If I go to http://127.0.0.1:8000/regulations I simply get:
Page not found (404)
How should I set up my urlpatterns?
urlpatterns = [
...
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
...
]
with path('', include(router.urls)), you can get:
http://127.0.0.1:8000/regulations/
http://127.0.0.1:8000/languages/
with
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
you can get:
http://127.0.0.1:8000/api-auth/{other paths}
After registering the router you have to include it in the urlpatterns. The way how #Ykh suggested is technically correct, but with regards to content is missing the point.
urlpatterns = [
# here you include your router
path('', include(router.urls)),
# here you include the authentication paths
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
Now you'll have the following routes:
http://localhost:8000/regulations/
http://localhost:8000/languages/
plus:
http://localhost:8000/api-auth/{other paths}