Seeing only one endpoint in browsable API instead of two - django

How come I can only see one of my URLs (users) instead of both in the django rest browsable API?
urls:
urlpatterns = [
path("admin/", admin.site.urls),
path("api-auth/", include("rest_framework.urls")),
path("", include("accounts.urls")),
path("", include("properties.urls")),
]
I expected to see users AND properties
accounts.urls:
from django.urls import path, include
from accounts.views import UserViewSet
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r"users", UserViewSet)
urlpatterns = [path("", include(router.urls))]
properties.urls:
from django.urls import path, include
from properties.views import PropertyViewSet, UnitViewSet
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r"properties", PropertyViewSet)
router.register(r"units", UnitViewSet)
urlpatterns = [path("", include(router.urls))]

Related

I am currently using django 4.0 getting this error while including ('rest_auth.urls')

from django.contrib import admin
from django.urls import path,include
from Users.urls import *
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path("",include("Users.urls")),
path('api/v1/rest-auth/', include('rest_auth.urls')),
]
Error:-ImportError: cannot import name 'url' from 'django.conf.urls'
In rest_auth.urls do this:
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
app_name = 'recipe'
urlpatterns = [
path('',include(router.urls)),
]
Hope your problem will solve now.

How to get rid of app name in the particular url?

I have this urls.py in my app
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from products import views
app_name = 'products'
router = DefaultRouter()
router.register(r'products', views.ProductViewSet, basename='products')
router.register(r'categories', views.ProductCategoryViewSet, basename='categories')
router.register(r'brands', views.BrandViewSet, basename='brands')
urlpatterns = [
path('', include(router.urls)),
]
And this is my project's urls.py
from django.contrib.auth import views as auth_views
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'),
name='login'),
path('logout/', auth_views.LoginView.as_view(template_name='users/logout.html'),
name='logout'),
path('__debug__/', include('debug_toolbar.urls')),
]
urlpatterns += [
...
path('products/', include('products.urls', namespace='products')),
...
]
And viewsets:
from rest_framework import viewsets, permissions
from .models import (
Product,
ProductCategory,
Brand,
)
from .serializers import ProductSerializer, ProductCategorySerializer, BrandSerializer
#all other viewsets are the same
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
A router for my app generates urls almost as expected, I can go to 'site/products/categories' for categories 'site/products/brands' for brands BUT for products url is 'site/products/products'. How to make it not to add app name in this case? I want it to be just 'site/products'.
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from products import views
app_name = 'products'
router = DefaultRouter()
router.register(r'categories', views.ProductCategoryViewSet, basename='categories')
router.register(r'brands', views.BrandViewSet, basename='brands')
router.register(r'', views.ProductViewSet, basename='products')
urlpatterns = [
path('', include(router.urls)),
]
Django is trying to match url to one url from list of router urls, so if you have two same urls, but one is faster in list, Django will pick always the first one.

How to remove last / character in root URL API in Django

API only work with url below:
http://127.0.0.1:1997/api/v1/groups/
How can I remove the last '/' so that it works like below:
http://127.0.0.1:1997/api/v1/groups
My config url code:
import os
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from rest_framework.schemas import get_schema_view
from rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRenderer
from groups import views as group_views
API_VERSION = os.getenv('API_VERSION')
API_ROOT = f"api/{API_VERSION}/"
router = routers.DefaultRouter()
router.register('groups', group_views.GroupViewSet)
schema_view = get_schema_view(
title='next_blog',
renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer])
urlpatterns = [
path('admin', admin.site.urls),
path('api_auth', include(
'rest_framework.urls', namespace='rest_framework')),
path('docs', schema_view, name='docs'),
path(API_ROOT, include(router.urls)),
]
Thanks everyone !
Set the trailing_slash argument to False when instantiating the router.
router = DefaultRouter(trailing_slash=False)
Django Rest Framework Default Router

How to resolve an error 404 for django-social?

I want to be able to login through social media. Followed all the steps (registered app), the login works just fine but does not go through because django does not recognize my url.
This is my call to the api endpoint
facebookLogin(token: string):any{
return this.http.post(environment.api + 'fblogin/', {token:this.token}).subscribe(
(onSucess:any) => {
localStorage.setItem(this._tokenKey, onSucess.token)
}, onFail => {
console.log(onFail)
}
);
}
But I get the following error : POST http://127.0.0.1:8000/api/fblogin/ 404 (Not Found). From this I know there to be something wrong with my django urls. And indeed going to http://127.0.0.1:8000/api/fblogin/ gave me a page not found error and that it tried to match several other urls.
However I can't see what is wrong with my urls
URLS in my app
from django.conf.urls import url, include
from rest_framework import routers
from . import views
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
from social_django import urls
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'fblogin/', include(urls)),
url(r'auth/', obtain_jwt_token),
url(r'refresh/', refresh_jwt_token)
]
URLS in my project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('Backend.api.urls'))
]
Other URLS like http://127.0.0.1:8000/api/users/ do work. I also am under the impression that all of my settings are in order.
I suggest that I might be because of the order and way in which you have defined URLs.py try the below format
from django.conf.urls import url, include
from rest_framework import routers
from . import views
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
from social_django import urls
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^fblogin/', include(urls)),
url(r'^auth/', obtain_jwt_token),
url(r'^refresh/', refresh_jwt_token)
] + router.urls

Django Rest Framework URL Mapping for multiple apps

I have a django-rest project called main and under it I have created an app called users. So, my project has the files :-
main/main/urls.py
and
main/users/urls.py
In users/urls.py I have
from django.conf.urls import url, include
from rest_framework import routers
from users import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
and in the main/main/urls.py I have
from django.conf.urls import url
from django.contrib import admin
from users import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users.urls),
]
However, I keep getting the error NameError: name 'users' is not defined. What is the correct way to set up urls when I have multiple apps? I would like to have a urls.py file for each app that is independent of the project. And in the root urls.py would include routing to different apps.
You import url not user, can try it
from users import urls as users_url
# ^^^^^^^^
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users_url),
# ^^^^^^^
]
but better:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', include('users.url')),
# ^^^^^^^
]
more details including-other-urlconfs