Category: Django Page 2 of 4

Cookie in Django | Django Tutorial

What is Cookie?

A cookie is a small piece of text file that contains data. The data in the cookie is often a random key or token. The server use a cookie to identify a client. Specific cookie known as HTTP cookie is used to identify a specific user and to keep track of that user’s movements within the site.

Suppose, you have visited a shop for the first time & bought some cloths of your favorite color. The owner has given you a membership card. Now, if you go again, he might not remember your face but he will recognize the card issued by his shop & can find your buying history & can serve you well. Here, the shop is the web server, you are the client and the membership card is the cookie. As the membership card will not be valid for other shops similarly a cookie won’t be valid for a different website.

Read More

Django Sessions | Django Tutorial

Communication between web browsers (clients) and servers is completed via HTTP( Hyper Text Transfer Protocol). It is a stateless protocol. This means a HTTP server does not keep track of any state information. The server will not remember whether a client has visited it before. Each time a client visits the server behaves with it as a new client. The sessions make a stateless protocol stateful.

Django has a session framework which handles cookies. In cookies we store session_key. It is a unique 32-character-long random string. A session_key can be considered as a token (a sequence of characters). It identifies a unique session within a particular web application. Sessions are used to abstract the receiving and sending of cookies, data is saved on server-side, and a session_key is stored in the client-side cookie for identification.

Read More

Custom Template Filter in Django | Django Tutorial

We have already discussed Django Template Tags. In this tutorial, we will work with Django Custom Template Filter.

What is a Template Filter in Django?

A template filter is simply a feature that can transform the value of a template variable before it is displayed.


{{ variable_name | filter_name }}

Built-In Django Template Filters

There are some inbuilt filters in Django. Let’s see some of them first.


To convert the value of a variable into lower case characters:

{{ variable | lower }}

If value is “Bangladesh”, the output will be “bangladesh”.

Read More

Django Authentication SignUpView -LoginView-LogoutView

In this tutorial, we’ll discuss ‘how to allow users to log in to a website with their own accounts and how to allow them to create accounts. The mechanisms work behind the scene is called authentication.

Authentication verifies a user if he is who he pretends to be, and authorization determines what an authenticated user can do.

Django by default has a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. We will discuss the following topics here:

  • Default User Model
  • Creating User
  • Creating Super User
  • Authenticating User
  • Authentication Views
    • Log In
    • Log Out
    • Sign Up / Registration

Read More

Django Custom Template Tags | Django Tutorial

Template tags are code snippets used to add functionality in templates. Django offers a variety of template tags such as {% if %}, {% for %}, {% block %}. Besides, Django allows us to create our own set of custom tags and make them available in the Django templates. We can extend the template engine by defining custom tags. Then it will be available to our templates. In this tutorial, we will discuss Django Custom Template Tags.

Why We Use Template Tags in Django

Suppose, we have an eCommerce website to build. It will contain multiple category products and we will have to show all the categories in our ‘navbar’. To do this we will have to pass the queryset ‘Categories.objects.all()’ from each of our views to show in each template. But, Using template tags we can skip this redundancy. We will make a custom template tag that will show all the categories and load it into our ‘base.html’ file.

Read More

Django Custom Model Manager | Model Manager

Django Custom Model Manager

In this article we are going to discuss Django Custom Model Manager & Django Default Model Manager. So, What is a manager?

A Manager is a bridge through which database queries talk with Django models. In other words, it is actually an interface which interacts with application and database. There is at least one Manager that exists for every model in a Django application named objects. It is the default manager of every model that retrieves data from the database.

In this article we will discuss about creating Custom Model Managers & why should we use them.

Read More

Django Form and ModelForm | Django Tutorial

Forms are basically used for taking input from the user. For adding, updating, retrieving that information to databases using GET or POST request. There is two forms classes in Django which is used to create HTML forms. Form & ModelForm. The difference between the Form & ModelForm class is that ModelForm class needs a model to create a form, but the Form class does not require a Model. We will discuss both here. Each field of the Form class map to the HTML form  <input>  element and each field has custom validation logic. Django does three distinct parts of the work involved in forms:

  • Prepares and Restructures data to make it ready for rendering
  • Creates HTML forms for the data.
  • Receives and Processes submitted data from the user.


Creating forms in Django is similar to creating a model. We just need to inherit from Django Form class and the attributes of the class will be the form fields. 

Read More

get_context_data() | Multiple Models data in a View

Did you face the problem of fetching data from multiple models in a single view while developing your Django application?

For example, Think of showing all the cars owned by a person in the person’s detail view. The generic DetailView provides the Person’s object to the context, but how do we get information of cars in that template? In this article, we will find out the solution.

The solution is to override the get_context_data() method of generic views. This method returns a dictionary. This method is used to populate the ‘context’ (can be named anything) dictionary to use as the template context.

Read More

Django Template Language | Templates Syntax

We have given a brief overview of Django Templates before. Now, we will dive deep into it. We will discuss the following topics today:

  • Django Template Language (DTL)
  • Accessing Dynamic Values
  • Looping Over Queryset objects
  • Using Conditional Statements in Templates
  • Setting URL Navigation
  • Template Inheritance
  • Loading Static

Django Template Language (DTL)

A Django template is a string of text which contains the static parts of HTML output and some special syntax describing how dynamic contents will be presented. Django has its own template System- The Django template language (DTL). But Django 1.8 also supports the popular alternative Jinja2.

The syntax of the Django template language contains four constructs. Variable, Tags, Filters, Comments. Some constructs are recognized and interpreted by the template engine. There are several Python template engines. A Django project can be configured with one or more template engines.

Read More

Django Views and URLs | Django Tutorial

What is view in Django?

A view is defined as a callable that accepts a web (HTTPRequest) request and returns a response (HTTPResponse). It is usually a python function or a python class with a special class method such as as_view().

Views typically does one or more things:

  • Returns plain text response
  • Returns HTML templates or any other formatted text.
  • Raises Exception
  • Talks to database via models

In Django, There are two types views:

  1. Function Based Views (FBV)
  2. Class Based Views (CBV)

Read More

Page 2 of 4

Powered by WordPress & Theme by Anders Norén