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

Related :   Solved: django.template.exceptions.TemplateSyntaxError: 'staticfiles' is not a registered tag library

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 –


lynxbee_ezoic

Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


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

Leave a Comment

Android Android Build system Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded