Category: Python Page 2 of 3

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.

forms.Form

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

Python Essentials | Python Basics Tutorial

In this Python tutorial, you will learn about some essentials of Python. The topics that we are going to discuss are:

  • Statements
  • Expressions
  • Multiline Statements
  • Indentation
  • Comments
  • Identifiers
  • Keywords
  • Naming Convention

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

Creating Relationships in Django Models

In our previous  Django Tutorial, we have discussed on Django models. Now we are going to discuss about Creating Relationships in Django Models. We are going to cover up the following topics.

1  Relationships in Django models
  • One To One (OneToOneField)
  • Many To Many (ManyToManyField)
  • One To Many (ForeignKey)
2  Data integrity options

Relationships in Django models

Django uses ORM (a library/tool)  that creates a bridge between Relational Database Tables and Python  Objects. It allows us to write Python code instead of SQL queries to run CRUD (Create, Read, Update and Delete) operations by providing a high-level abstraction upon a relational database.

Read More

Python Variables( Global, Local, Nonlocal) , Constants

Hellow Programmers, In this python tutorial I am gonna talk about variables & their types. So What is Variable?

Variable: Think about an empty pot in which you can put anything. In programming we call it variable. Actually a variable is an empty memory location to store values.

How to declare a variable in python?

Declaring a variable in python is a piece of cake. Name it & put a value into it. As python is a dynamically typed language we don’t need to declare variable types. Python is smart enough to detect the type itself.

There are some rules of declaring variables:

  • A variable name must start with a letter or the underscore character.
  • Cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores. For example, anything like this is valid: A-z, 0–9, and _
  • Variable names are case-sensitive (height, Height, and HEIGHT are three different variables names)

Read More

Python Dictionaries – All You Need To Know

Hello geeks, in this python 3 tutorial, you are going to learn A to Z of Python dictionary. We will discuss the followings:

  • What is Python Dictionary?
  • How to create a Dictionary!
  • Add, Delete, Update Elements.
  • Access Elements of a Dictionary.
  • Merging Two Dictionaries
  • Creating Nested Dictionary
  • Adding, Deleting, Updating elements in a Nested Dictionary.
  • Different Methods of Python Dictionary.
  • copy(), fromkeys(), get(), pop(), popitem(), items(), keys(), values()

So, without further ado let’s jump into it.

What is Python Dictionary?

Python dictionary is a data type of python programming language. It stores data by mapping them with a key. Data are stored in “Key-Value” pairs where each “Key” must be unique. Lists, Tuples can also be stored as value in a dictionary.

Read More

Page 2 of 3

Powered by WordPress & Theme by Anders Norén