How to define different urlpatterns in view.py for Django

As we have seen in our previous post “Developing REST API using functions in DRF application views” we defined two URL’s,

  1. http://127.0.0.1/users
  2. https://127.0.0.1/user/user_primary_key => example : http://127.0.0.1/user/2

The first URL, “http://127.0.0.1/users” is mapped with view in url.py with urlpattern and in view.py as function “users” as,

urlpatterns = [
    path('users/', views.users),
]
@csrf_exempt
def users(request):
    if request.method == 'GET':
        users = UserInfo.objects.all()
        serializer = UserInfoSerializer(users, many=True)
        return JsonResponse(serializer.data, safe=False)

The second URL, http://127.0.0.1/user/2 , where 2 is the primary key of the objects is mapped with urlpatterns in url.py as,

urlpatterns = [
    path('user/<int:pk>/', views.user),
]

and this url is mapped with view.py function “user” as,

@csrf_exempt
def user(request, pk):
    if request.method == 'GET':
        users = UserInfo.objects.filter(pk=pk)
        serializer = UserInfoSerializer(users, many=True)
        return JsonResponse(serializer.data, safe=False)

Now, below we will try to list few more url patterns which can be defined if we want to use the different url for accessing the information from server.

3. http://127.0.0.1/user_by_uuid/UUID => example … http://127.0.0.1/user_by_uuid/ecbba534-e23b-11ea-96a2-0744224669b7

If we have following JSON, where user_id needs to be UUID,

{
	"id": 5,
	"userid": "ecbba534-e23b-11ea-96a2-0744224669b7",
	"username": "lynxbee1",
	"email": "social(at)lynxbee.com",
	"age": "41"
}

then he URL to GET information of user id “ecbba534-e23b-11ea-96a2-0744224669b7” can be accessed using URL http://127.0.0.1/user/ecbba534-e23b-11ea-96a2-0744224669b7 and this can be mapped with urlpatterns as,

urlpatterns = [
    path('user/<uuid:user_id>/', views.user),
]

The related view.py function will be,

@csrf_exempt
def user(request, user_id):
   if request.method == 'GET':
        users = UserInfo.objects.filter(userid=user_id)
        serializer = UserInfoSerializer(users, many=True)
        return JsonResponse(serializer.data, safe=False)

4. http://127.0.0.1/user_by_name/username_as_string => example … http://127.0.0.1/user/lynxbee1

This uses the same JSON as above for example, then url.py needs to defined as,

urlpatterns = [
    path('user/<str:user_name>/', views.user_by_name),
]

and related views.py function needs to defined as,

@csrf_exempt
def user_by_name(request, user_name):
    if request.method == 'GET':
        users = UserInfo.objects.filter(username=user_name)
        serializer = UserInfoSerializer(users, many=True)
        return JsonResponse(serializer.data, safe=False)

5. http://127.0.0.1/user/slug => example : http://127.0.0.1/user/show-the-userinformation

Django urlpattern also supports using complete “slug” to access the views.. hence for example, if we want to design a page and access it using url “http://127.0.0.1/user/show-the-userinformation” then we need to define urlpatten in url.py as,

urlpatterns = [
    path('user/<slug:custom_slug>', views.index_slug),
]

And is respective implementation in views.py as,

@csrf_exempt
def index_slug(request, custom_slug):
    return HttpResponse("This page accessed using Slug : " + custom_slug)

6. Accessing index page http://127.0.0.1/user/

For designing index page which may give some generic information about user API, we need to modify urlpattern as,

urlpatterns = [
    path('user/', views.index),
]

and its respective views.py implementation as,

@csrf_exempt
def index(request):
    return HttpResponse("This is Index page of user")

References –

1 thought on “How to define different urlpatterns in view.py for Django”

Leave a Comment