Django REST Framework URLs with Django 2.0 - django

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}

Related

Is it possible to include djoser urls and custom model urls in one querystring?

urlpatterns = [
path('users/', include('users.urls')),
path('users/', include('djoser.urls')),
path('users/', include('djoser.urls.jwt')),
]
it gives me error when I try to include djoser.urls and users.urls in one path. Are there any options to do this?

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"))
]

why 404 in django url?

I made url for signUp page.
but it returns 404 error.
all of the other urls work well.
I don't know the reason.
main urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('mobileWeb.urls')),
path('api/', include('api.urls')),
]
Application urls
urlpatterns = [
path('', views.index, name='index'),
path('index', views.index, name='index'),
path('addComment', views.addComment, name='addComment'),
# users
path('signUp', views.signUp, name='signUp'),
path('accounts/', include('allauth.urls')),
path('martDetail/<int:martId>', views.martDetail, name='martDetail'),
path('trade/<int:itemId>', views.trade, name='trade'),
path('registerMart', views.registerMart, name='registerMart'),
path('registerItem', views.registerItem, name='registerName'),
path('delete', views.delete, name='delete'),
path('deleteMart', views.deleteMart, name='deleteMart'),
path('deleteItem', views.deleteItem, name='deleteItem'),
path('purchaseItem', views.purchaseItem, name='purchaseItem'),
path('selectItem', views.selectItem, name='selectItem'),
path('addStatistics', views.addStatistics, name='addStatistics'),
path('viewStatistics', views.viewStatistics, name='viewStatistics'),
path('imtPosRegister', views.imtPosRegister, name='imtPosRegister'),
path('imtPosRegisterTest', views.imtPosRegisterTest, name='imtPosRegisterTest'),
path('imtPosSaleInfoTest', views.imtPosSaleInfoTest, name='imtPosSaleInfoTest'),
path('imtPosSaleConfirmTest', views.imtPosSaleConfirmTest, name='imtPosSaleConfirmTest'),
path('fsOe9ms1b', views.fsOe9ms1b, name='fsOe9ms1b'),
path('fsOe9ms1b_ma', views.fsOe9ms1b_ma, name='fsOe9ms1b_ma'),
path('ssOe9ms1b', views.ssOe9ms1b, name='ssOe9ms1b'),
path('ssOe9ms1b_ma', views.ssOe9ms1b_ma, name='ssOe9ms1b_ma'),
path('tsOe9ms1b', views.tsOe9ms1b, name='tsOe9ms1b'),
path('tsOe9ms1b_ma', views.tsOe9ms1b_ma, name='tsOe9ms1b_ma'),
path('writeChatting', views.writeChatting, name='writeChatting'),
path('imageUploadChatting', views.imageUploadChatting, name='imageUploadChatting')
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
404 in web browser
404 in console
The url path matching is case-sensitive. In your paths you wrote:
path('signUp', views.signUp, name='signUp'),
with uppercase U in signUp. I would however advise to use only lowercase, and rewrite this to:
path('signup/', views.signUp, name='signUp'),
The same with other paths in your urls.py.
Try visiting http:\127.0.0.1:8000\signUp

How to disable django REST framework docs page

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'))
]

How to set URL include to answer root level urls in django

I have these URL files:
project/url:
urlpatterns = [
url(r'^$', include(public)), <--- URL IN ERROR!!!!
url(r'^member/', include(mviews)),
url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
public/url:
urlpatterns = [
url(r'^$', views.index),
url(r'^login/', views.login)
]
mviews/url:
urlpatterns = [
url(r'^$', views.index),
url(r'^api/', include(router.urls)),
]
in the first URL file, the first URL where it says include(public) is erroring out. How do I set it so public is the "home" url group? Thanks.
I think here you have typo, your the name of file is wrong(url.py), It should be urls.py
And if above line holds then you can achieve grouping urls from public module by following
url(r'^home/', include('public.urls')),
url(r'^member/', include(mviews)),
url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))