Learn the basics of Django, a powerful web framework for Python.
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It’s built by experienced developers and takes care of much of the hassle of web development, allowing you to focus on writing your app without reinventing the wheel.
Before diving into Django, make sure you have a basic understanding of the following:
To install Django, you can use pip, Python's package installer. Run the following command in your terminal:
pip install django
After installation, verify the version of Django installed:
django-admin --version
Let's create a simple Django project and app to display "Hello World" on the webpage.
django-admin startproject myproject
cd myproject
python manage.py startapp hello
Add your app to the INSTALLED_APPS
in settings.py
:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
...
'hello',
]
In views.py
, add a view to display "Hello World":
from django.http import HttpResponse
def hello_world(request):
return HttpResponse("Hello, World!")
In urls.py
, map the URL to the view:
from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.hello_world),
]