Basics of Django
Django is a high-level Python web framework that allows developers to create web applications quickly and easily. It follows the Model-View-Controller (MVC) pattern and emphasizes reusability and rapid development.
Syntax
To run a basic Django project, the following syntax is used:
python manage.py runserver
This command starts the server at http://127.0.0.1:8000/
.
Example
Let's create a simple Django application that displays "Hello, World!" on the web page.
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello, World!")
Output
When we run the server, and navigate to http://127.0.0.1:8000/hello
, we will see "Hello, World!" displayed on the page.
Explanation
In the above example, we define a function called hello
that returns an HttpResponse
object with the "Hello, World!" message. We map the hello
function to the URL /hello
using the urls.py
file.
Use
Django can be used to create web applications for a variety of purposes, such as:
- Building e-commerce sites
- Developing social networking applications
- Creating content management systems
- Building web-based scientific tools
Important Points
- Django follows the Model-View-Controller (MVC) pattern.
- It emphasizes reusability and rapid development.
- It provides built-in tools for authentication, content administration, and caching.
Summary
In this section, we learned about the basics of Django, such as its syntax, example, output, explanation, use, important points, and summary. Django is an excellent tool for creating web applications quickly and easily. With Django, developers can focus on writing applications without worrying about the underlying infrastructure.