Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It helps you build robust, scalable, and secure web applications quickly by handling common tasks like URL routing, database interaction, templates, and authentication for you.
In this tutorial, you will see how to install Django, create a project, create an app, and display a simple “Hello, Django!” page in the browser.
/ or /about/) to a specific view.A basic Django flow looks like this:
urls.py.views.py.HttpResponse object (often with HTML content).You organize your code into apps, and each app can have its own views.py, urls.py, models, templates, and more.
First, install Django (preferably inside a virtual environment):
# Install Django in your environment
pip install django
Use django-admin to create a new project, move into the project folder, and run the development server:
# Create a project, enter it, and start the dev server
django-admin startproject myproject
cd myproject
python manage.py runserver
After running the server, open http://127.0.0.1:8000/ in your browser. You should see the default Django welcome page.
Inside your project, create an app where you will write your views and other logic:
# Create a new Django app called "myapp"
python manage.py startapp myapp
Remember to add 'myapp' to INSTALLED_APPS inside settings.py so Django knows about it.
Now let’s define a simple view and connect it to a URL. This will display "Hello, Django!" in the browser.
# myapp/views.py
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!")
# myproject/urls.py
from django.urls import path
from myapp import views
urlpatterns = [
path('', views.home),
]
When you run:
# Start the Django development server
python manage.py runserver
and open http://127.0.0.1:8000/ in your browser, Django uses the URL patterns in myproject/urls.py to match the empty path ''. This points to views.home in myapp/views.py.
The home function receives the request and returns an HttpResponse with the text: "Hello, Django!". That text is what you see rendered in the browser as a simple web page.
INSTALLED_APPS in settings.py so Django can load them.urls.py from app-level urls.py to avoid confusion.python manage.py makemigrations and python manage.py migrate) before using models.about) and map it to a new URL like /about/.