Django REST Framework JWT Authentication

Ath Tripathi
Updated on 25-Aug-2021 13:11:15

2K+ Views

If you ever worked with Django REST framework, then you surely know about JWT authentication. JWT authentication is used for token authentication and it is really a popular method for authentication in Django. JWT stand for JSON Web Token. Let's see how to work with it.First, install a package −pip install djangorestframework-simplejwt pip install djangorestframeworkWe are not going to need an App for this, we will just do a basic setup for Django REST framework frontend and authentication backend.ExampleIn settings.py, add the following −INSTALLED_APPS = [ ... 'rest_framework_simplejwt', 'rest_framework' ... ] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication',    ) }Here, ... Read More

Django Query Count in Terminal for Debugging

Ath Tripathi
Updated on 25-Aug-2021 13:09:29

794 Views

In this article, we are going to use a library in Django to see a brief report of database query count in terminal which can be used for debugging. It will provide a brief tabular report of every hit on any model object and print it on every hit, whether it is read or write. It will also calculate the response and request.ExampleDo some basic stuff like setting apps and urls.Install the django-querycount module −pip install django-querycountIn settings.py, add this −MIDDLEWARE += [    'querycount.middleware.QueryCountMiddleware', ]This will enable the functionality of library and allow us to use it at runtime ... Read More

Django Model Object Hit Counting

Ath Tripathi
Updated on 25-Aug-2021 13:06:04

515 Views

In this article, we are going to make a project in which we will see how to make a Django table that will store all the data of hit count and detailed data of hit. It can be used generate useful reports. So, let's get started.Setup your urls.py and install the django-hitcount modulepip install django-hitcountExampleIn settings.py, add the following line −INSTALLED_APPS+ = ['hitcount']Here we added this library as an app in the project.Run the following commands −python manage.py makemigrations python manage.py migrateHere, we create migrations and then we migrate it.That's all. This library will create a table that will store ... Read More

Django Model Data to JSON Data in 2 Lines

Ath Tripathi
Updated on 25-Aug-2021 13:04:38

2K+ Views

In this article, we are going to learn a handy trick to convert Django model data directly to JSON data. Sometimes, we need to return model data in JSON format; it can also be used in making API or just showing simple data to our frontend in JSON format. JSON is easy to access so it is really handful.ExampleCreate a Django project and an app.In settings.py, add the app name in INSTALLED_APPS.In urls.py of the Project main directory, add the following lines −from django.contrib import admin from django.urls import path, include urlpatterns = [    path('admin/', admin.site.urls),    path('', ... Read More

Django Admin Based File Management

Ath Tripathi
Updated on 25-Aug-2021 13:00:16

1K+ Views

We usually make file or image related changes from the frontend. In this article, we will see how to make file management at Admin panel where we can manage different types of file that we are going to render from server to client.ExampleInstall the django-filer module −pip install django-filerIn settings.py, add the following −INSTALLED_APPS = [    ...    'easy_thumbnails',    'filer',    'mptt',    ... ] THUMBNAIL_HIGH_RESOLUTION = True THUMBNAIL_PROCESSORS = (    'easy_thumbnails.processors.colorspace',    'easy_thumbnails.processors.autocrop',    #'easy_thumbnails.processors.scale_and_crop',    'filer.thumbnail_processors.scale_and_crop_with_subject_location',    'easy_thumbnails.processors.filters', )Here, we have just added the necessary libraries as apps in our project. We added ... Read More

Creating a Screenshot Taking Website in Django

Ath Tripathi
Updated on 25-Aug-2021 12:55:43

904 Views

In this article, we will see how to create a screenshot taking website in Django. This website will basically take the screenshot of our screen whenever we click "take screenshot". It then will store the screenshot in our media folder and we can view it whenever we want.ExampleSo let's start by creating a project and an app.In settings.py, in INSTALLED_APPS add your app name, and add this code at the bottom −MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR/'media'We just setup our basic media folder for our image uploading purpose.Now in project's urls.py −from django.contrib import admin from django.urls import path, include ... Read More

Client-Side Image Zooming and Rotating in Django

Ath Tripathi
Updated on 25-Aug-2021 12:50:15

687 Views

Sometimes, we may need to rotate an image or zoom it. In such cases, a client-side image zooming library is used to add jquery feature of zooming-rotating on html file. We just needed to load jquery js and css in html file.Create a Django project and an app. Setup urls and do some basic stuff like adding app in INSTALLED_APPS.Install the django-client-side-image-cropping librarypip install django-client-side-image-croppingNow in settings.py, add the following line −INSTALLED_APPS+=['client_side_image_cropping']Here, we have done a simple setting of adding a module as an app.ExampleIn models.py, add the following lines −from django.db import models # Create your models here. ... Read More

Adding Translation to a Model Instance in Django

Ath Tripathi
Updated on 25-Aug-2021 12:44:25

326 Views

In this article, we are going to learn how to create a translation for any instance. Sometimes, you may need to save data like ID, names, quotes, lines, etc. You may have to render that data in different languages; for that, you need to do a lot of database stuff, but today I will show you how to get the same result in just a few lines of setup.Create a Django project and an app. Setup urls and do some basic stuff like adding app in INSTALLED_APPS.Create a model. Here, we don't have much to do with views.py, urls.py or ... Read More

Add a Money Field in Django

Ath Tripathi
Updated on 25-Aug-2021 12:37:19

2K+ Views

Sometimes, we may have to add money-related data in a website, like salary, fees or income. Django provides an integer field but many a time, it doesn't work like we want. So, for handling money field, we can use a third-package library that will add the money field to our model.Make a project and an app, I named it "MoneyFieldDemo" and "myapp".Set the basic things like urls and INSTALLED_APPS.And yes, install a library −pip install django-moneyAdd the following line in settings.py −INSTALLED_APPS+= ["djmoney"]ExampleIn app's, urls.py, add the following lines −from django.urls import path from . import views urlpatterns = ... Read More

Adding JSON Field in Django Models

Ath Tripathi
Updated on 25-Aug-2021 12:33:02

15K+ Views

In this article, we will see how to add JSON fields to our Django models. JSON is a simple format to store data in key and value format. It is written in curly braces. Many a time, on developer website, we need to add developer data and JSON fields are useful in such cases.First create a Django project and an app. Please do all the basic things, like adding app in INSTALLED_APPS and setting up urls, making a basic model and render its form in an HTML file.ExampleInstall the django-jsonfield package −pip install django-jsonfieldNow, let's create a model in models.py, ... Read More

Advertisements