Django rest framework browsable api root url - python

I love DRF but I still find its documentation really frustrating, for real, a lot of advanced tips and examples are there but I still always dive into toughness and frustration when I'm looking for the most basic stuff of any topic of the amazing framework.
In this case, this resource
looks really cool! But they don't even start by how to make it work (or what could not be properly configured if it isn't working as in my case). Could someone tell me what's the holy api root URL or how to include/configure it? Because I don't see it in my URLs and so far I didn't find anything in the official docs.
I also followed the quickstart tutorial.
This is what I have:
My main urls.py:
api_urls = [
path('rest_framework/', include('rest_framework.urls', namespace='rest_framework')),
path('', include('project.posts.urls')),
path('', include('project.users.urls')),
]
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include(api_urls)),
]
users.urls:
router = SimpleRouter()
router.register(r'users', UserViewSet, basename='users')
urlpatterns = [
path('', include(router.urls)),
]
posts.urls:
router = SimpleRouter()
router.register(r'posts', PostViewSet, basename='posts')
urlpatterns = [
path('', include(router.urls)),
]
I can log in into the DRF panel but then I get a redirection error.
Also I can see the pages if I manually go to a valid endpoint URL, but it's not an actual navigation from the root point of the API.

Related

when I change admin URL, my project API URL not found. how can I solve it?

when I change the URL link
path("admin/", admin.site.urls),
to
path("", admin.site.urls), it works fine
but raises API Not Found Error when I have the following urlpatterns.
I need path("api/", include("config.api_router")),
Note: when I use path("admin/", admin.site.urls), API works
Django URL dispatcher runs through each URL pattern, in order, and stops at the first one that matches the requested URL. Try switching the order:
urlpatterns = [
path("api/", include("config.api_router")),
# other urls
path("", admin.site.urls)
]

Django Home url set to folder

I have an issue where I would like my address (whether local or live) to point to my projects app as my home page, yet every change I make breaks the other URLs. My main site URL patterns are:
urlpatterns = [
path('admin/', admin.site.urls),
path("projects/", include("projects.urls")),
path("blog/", include("blog.urls")),
]
I have tried to change the path("project/", include("projects.urls")) to path("", include("projects.urls")) which breaks the blog index.
my blog has the following pattern:
urlpatterns = [
path("", views.blog_index, name="blog_index"),
path("<slug:slug>/", views.blog_detail, name="blog_detail"),
path("<category>/", views.blog_category, name="blog_category"),
]
And my projects:
urlpatterns = [
path("", views.project_index, name="project_index"),
path("<slug:slug>/", views.project_detail, name="project_detail"),
path("project/<tag>/", views.project_tag, name="project_tag"),
]
The conflict is coming from django not knowing the difference between:
http://example.com/blog
and
http://example.com/(project_slug_called_blog)
because they look functionally identical, just being a string.
You could reverse the order of the URLs so that blog is looked for first
urlpatterns = [
path('admin/', admin.site.urls),
path("blog/", include("blog.urls")),
path("", include("projects.urls")),
]
And that should work so long as not projects are called 'blog'
Or, failing that, you can make the project detail view's URL more explicit
path("<slug:slug>/view-details", views.project_detail, name="project_detail"),
I'd personally go with the second option as it makes the URL more human readable and is likely a more robust solution.

URLs not matching when admin page set to root url in django app

I building a Django app with DRF, but the API URLs do not match correctly when I set the admin site at the root URL
root URL pattern:
urlpatterns = [
path('', admin.site.urls),
path('api/auth/', include("account.urls")),
path('api/main/', include("main.urls")),
]
main URL pattern:
router = routers.DefaultRouter()
router.register('language', LanguageView)
urlpatterns = [
path('', include(router.urls)),
]
when I hit the main API URLs it returns the admin login page, can't fix this yet
please help me out.
Include the admin as the last entry in your urls, otherwise your api urls will never match because they are "valid" admin paths and will always be handled by the admin
urlpatterns = [
path('api/auth/', include("account.urls")),
path('api/main/', include("main.urls")),
path('', admin.site.urls),
]

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

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

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