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,

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

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,

[bash] urlpatterns = [ path(‘user//’, views.user), ] [/bash]

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

[bash] @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) [/bash]

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,

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

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,

[bash] urlpatterns = [ path(‘user//’, views.user), ] [/bash]

The related view.py function will be,

[bash] @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) [/bash]

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,

[bash] urlpatterns = [ path(‘user//’, views.user_by_name), ] [/bash]

and related views.py function needs to defined as,

[bash] @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) [/bash]

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,

[bash] urlpatterns = [ path(‘user/‘, views.index_slug), ] [/bash]

And is respective implementation in views.py as,

[bash] @csrf_exempt def index_slug(request, custom_slug): return HttpResponse(“This page accessed using Slug : ” + custom_slug) [/bash]

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,

[bash] urlpatterns = [ path(‘user/’, views.index), ] [/bash]

and its respective views.py implementation as,

[bash] @csrf_exempt def index(request): return HttpResponse(“This is Index page of user”) [/bash]

References –

Leave a Comment