django rest framework: routers.DefaultRouter() url with custom path - python

I want to define a path to access a certain api. What works so far (urls.py):
router = routers.DefaultRouter()
router.register(r'test', views.TestViewSet)
urlpatterns = patterns('',
url(r'^api/', include(router.urls)),
)
What i would like to do is adding a new ViewSet which provides "subfunctionality" of test (urls.py):
router.register(r'test/add', views.TestNewViewSet)
But this does not work. When accessing this api, all i get is a "404 Not Found" error. No exceptions are thrown when accessing the api. So what is wrong?
Any help is appreciated!

Try with
urlpatterns = patterns('',
url(r'^api/', include(router.urls)),
url(r'^test/add/$', TestNewViewSet.as_view(), name='whatever'),
)

Related

Python Page 404 not found

New to Python, just starting up with a Issue reporting app which involves an API module.
My Urls.py file:
urlpatterns = patterns('',
url(r'^api/', include('api.urls')),
)
My api.urls file
urlpatterns = patterns('api.v1',
# Examples:
# url(r'^$', 'newproject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
# authentication / session managemen
url(r'^auth/profile/me/$', 'account', name='my-account'),
url(r'^auth/profile/$', 'new_account', name='new-account'),
url(r'^auth/session/(?P<key>[a-z0-9]{64})/$', 'session', name='existing-session'),
url(r'^auth/session/$', 'new_session', name='new-session'),
....
My Web Page 404 Error
Using the URLconf defined in newproject.urls, Django tried these URL patterns, in this order:
^api/
The current URL, , didn't match any of these.
I might be overlooking a mistake I made...
Help Please?
Using Django 1.9.7
replace url(r'^api/', include('api.urls')),
with url(r'', include('api.urls')),

DefaultRouter class not creating API root view for all apps in python

I'm creating a simple Python API with two apps in it named as snippets and polls. For single entry point to my API, I'm using DefaultRouter class instead of a regular function-based view and the #api_view decorator.
As stated in Django Rest Framework Tutorial that
The DefaultRouter class automatically creates the API root view
I'm having issue in API root view. Here is my snippets/urls.py
router = DefaultRouter()
router.register(r'snippets', views.SnippetViewSet)
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
this is my polls/urls.py
router = DefaultRouter()
router.register(r'actors', views.ActorViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
My /urls.py is as following
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('snippets.urls')),
url(r'^', include('polls.urls')),
url(r'^api-auth/', include('rest_framework.urls',
namespace='rest_framework')),
]
Now when I start server and load app in browser, it only shows snippets' url as entry point like this (polls' url is missing)
and if change my root urls.py like this (first add polls.urls and then snippets.urls)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('polls.urls')),
url(r'^', include('snippets.urls')),
url(r'^api-auth/', include('rest_framework.urls',
namespace='rest_framework')),
]
Now it shows polls' urls (missing snippets urls)
But I want both apps' urls over there. I don't know what I'm missing here. Any kind of help will be appreciated.
Your url.py can look like this (To create a custom documentation page)
from django.urls import path, include
from rest_framework import routers
from rest_framework.response import Response
from rest_framework.views import APIView
from . import views
class DocsView(APIView):
"""
RESTFul Documentation of my app
"""
def get(self, request, *args, **kwargs):
apidocs = {'api1title': request.build_absolute_uri('api1endpoint/'),
'api2title': request.build_absolute_uri('api2endpoint/'),
'api3title': request.build_absolute_uri('api3endpoint/'),
'api4title': request.build_absolute_uri('api4endpoint/'),
}
return Response(apidocs)
router = routers.DefaultRouter()
router.register('api1endpoint', views.API1ViewSet)
router.register('api2endpoint', views.API2ViewSet)
urlpatterns = [
path('', DocsView.as_view()),
path('', include(router.urls)),
path('api3endpoint/', views.API4View.as_view()),
path('api4endpoint/', views.API4View.as_view()),
]
You shouldn't have same regex patterns that map to different views in the urlpatterns. When this happens, the server will just use the first matched pattern and routes to the first matched view.
Other alternative maybe you can create the default router and registers the two views in the root folder instead so that it can handle the api_rootview for both viewsets.
http://www.django-rest-framework.org/api-guide/routers/#defaultrouter

Integrate Django admin login as REST framework login

I want to use the admin page as 2 section
Admin site
Rest Framework API
After login into the admin only I have to access the REST API too.
Project Url is,
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', include('snippets.urls')),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) )
My App Url is
urlpatterns = [
url(r'^snippets/$', views.snippet_list),]
You can do something like this.In app urls.py by wrapping login_required decorator
from django.contrib.auth.decorators import login_required
urlpatterns = [
url(r'^snippets/$', login_required(views.snippet_list)),]
you need use real functions instead of their names.(its ok now)
OR
Use middleware, to check whether user is authenticated.

Django URL not working

Going to localhost/app works, but localhost/app/upload does not. Am I doing something really obviously wrong? I've tried a few different methods following the documentation without luck. I get a 404 saying the URL pattern does not match.
/project/app/urls.py
from app import views
urlpatterns = patterns('',
url(r'^$', views.view, name='result'),
url(r'^upload/$', views.upload, name='upload'),
)
/project/urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^app/$', include('app.urls')),
)
Don't terminate the inclusion URL.
url(r'^app/', include('app.urls')),

Django admin custom view and urls

I'm new to Django and I currently have two problems that I can't figure out from reading online:
URLs... I have an app 'cq' and the project is 'mysite' here is what I have in mysite's urls.py
urlpatterns = [
url(r'^cq/', include('cq.urls')),
url(r'^admin/', include(admin.site.urls)),
]
and this is what I have in cq's urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<team_id>[0-9]+)/$', views.team, name='team'),
]
However I don't want do to cq/team_id. I just want to be able to go directly to /team_id. Is there any way to do it?
I want an entry in the admin view of the database to look very custom, i.e. I want to write my own html.. How do I do it?
Thanks!
When you define
url(r'^cq/', include('cq.urls')),
it means all urls in cq.urls will have to start with cq/. For you case, just do
urlpatterns = [
url(r'^cq/', include('cq.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<team_id>[0-9]+)$', views.team, name='team') #without trailing slash for /team_id, but with for /team_id/
]
Of course, you don't need this rule anymore in cq urls.py

Categories