django
  1. django-template

Django Fundamentals

Heading h1

Django is a high-level web framework that eases the creation of complex database-driven websites. It uses the Python programming language, and it allows developers to quickly develop and deploy web applications.

Syntax

Here's the basic syntax of creating a Django project:

django-admin startproject project_name

And, to create an app inside a project, use this syntax:

python manage.py startapp app_name

Example

Let's create a simple Django app which displays "Hello World" on the home page.

  1. First, we create a new Django project called myproject:
django-admin startproject myproject
  1. Inside the myproject directory, create a new app called myapp:
python manage.py startapp myapp
  1. Open the views.py file inside the myapp directory, and add the following code:
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello World")
  1. Open the urls.py file inside the myproject directory, and add the following code:
from django.urls import path
from myapp.views import home

urlpatterns = [
    path('', home, name='home'),
]
  1. Finally, start the app by running this command:
python manage.py runserver

Output

Now, if you browse to http://localhost:8000/ in your web browser, you should see "Hello World" displayed on the screen.

Explanation

In the above example, we created a new Django project called myproject, and then we created a new app inside that project called myapp.

We then created a new view function inside myapp/views.py called home, which simply returns an HttpResponse with the message "Hello World".

We then mapped the home view function to the root URL of the app (/) in the myproject/urls.py file, and finally we started the app using the python manage.py runserver command.

Use

Django is a powerful and flexible web framework that can be used for a wide variety of applications. It is well-suited to rapid application development, and it has a large and active community of developers and contributors.

Some common use cases for Django include building e-commerce websites, content management systems, social networks, educational platforms, and more.

Important Points

  • Django is a powerful web framework that uses Python and enables rapid web application development
  • A Django project consists of one or more apps, each with its own set of views, models, and templates
  • Views are Python functions that handle requests and return responses
  • Models are Python classes that define the structure and behavior of the data in your web application
  • Templates are HTML files that provide the structure and layout of your web pages
  • Django uses the Model-View-Template (MVT) architecture to separate the different components of the application
  • Django includes many built-in features and tools for security, authentication, caching, and more

Summary

Django is a popular and powerful web framework that uses Python to enable rapid web application development. It includes a variety of features and tools for building complex, database-driven web applications, and it is well-suited to a wide variety of use cases. By separating the various components of the application into views, models, and templates, Django makes it easy to organize and structure your code, while also providing many built-in features for security, authentication, and more.

Published on: