Learn your Impossibilities with
Learn Cave

Learn Cave provides you best and easy learning tutorials, projects from scratch and developing programming skills from basics with user friendly lessons, Learning is only hard untill you are BEGINNER!

Hero
winning Cup

We provide Co-Curricular events for college students can explore our knowledges to outside of campus
By Learn Cave Team

Django

Introduction

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Django makes it easier to build better web apps quickly and with less code.

Django – Design Philosophies

Django comes with the following design philosophies

Overview

As you already know, Django is a Python web framework. And like most modern framework, Django supports the MVC pattern. First let's see what is the Model-View-Controller (MVC) pattern, and then we will look at Django’s specificity for the Model-View-Template (MVT) pattern.

MVC Pattern

When talking about applications that provides UI (web or desktop), we usually talk about MVC architecture. And as the name suggests, MVC pattern is based on three components: Model, View, and Controller. Check our MVC tutorial here to know more.

DJANGO MVC - MVT Pattern

The Model-View-Template (MVT) is slightly different from MVC. In fact the main difference between the two patterns is that Django itself takes care of the Controller part (Software Code that controls the interactions between the Model and View), leaving us with the template. The template is a HTML file mixed with Django Template Language (DTL).

Creating Views

A view function, or “view” for short, is simply a Python function that takes a web request and returns a web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image, etc. Example: You use view to create web pages, note that you need to associate a view to a URL to see it as a web page.

In Django, views have to be created in the app views.py file.

Simple view

We will create a simple view in myapp to say "welcome to my app!"

Snippet here

from django.http import HttpResponse
def hello(request):
text = """

welcome to my app !

"""
return HttpResponse(text)

In this view, we use HttpResponse to render the HTML (as you have probably noticed we have the HTML hard coded in the view). To see this view as a page we just need to map it to a URL (this will be discussed in an upcoming chapter).

We used HttpResponse to render the HTML in the view before. This is not the best way to render pages. Django supports the MVT pattern so to make the precedent view, Django - MVT like, we will need −

A template: myapp/templates/hello.html

from django.shortcuts import render
def hello(request):
return render(request, "myapp/template/hello.html", {})

Views can also accept parameters −

from django.http import HttpResponse
def hello(request, number):
text = "welcome to my app number %s!"% number
return HttpResponse(text)

About Us

About Us

Learn Cave helps you to get close to your dreams of Programming world. Learn Cave is one of the best online coding Learning platform for programmers in whole wide world. Beginner's who want to learn Coding and Programming are too joining us. We have around
With our comprehensive course curriculum, you will cover all cutting edge topics. Learn Cave has it's own brand, with TAG of Asia's Biggest Online Coding Course. With the evolution of computers programming languages, Learn Cave is also contributing in this evolution by provind paid courses of Programming Languages such as Python, Javascript, C++, Java, Angular and more. We help students to fast-track your career in this evolving programming field.

FAQ

Will Learn Cave provide course for freshers in programming?

Yes, our courses are too friendly to get understood easily for freshies in programming.

How can I join codefest of Learn Cave?

Subscribe our Learn Cave Youtube to get instant updates of codefest. You can also join Our Discord Server for more information.

Why should I choose Learn Cave when other platforms provide course in less amount?

Learn Cave provides you 24/7 Guidence in programming courses. Users get free source code of many websites for Professional/Advance Learning.