Django Project and App Structure
• Project: A Django project is a collection of settings and c
django-admin startproject project_name
App: A Django app is a web application that performs a speci c task within a project. You can
create an app using:
python manage.py startapp app_name
• A project can contain multiple apps.
Settings.py
3. Views
Views handle the logic behind web pages and send back responses (HTML, JSON, etc.). A basic
view looks like this:
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!”)
App urls.py
To map this view to a URL, modify urls.py:
from django.urls import path
from .views import home
urlpatterns = [
path('', home),
]
Here's a brief overview of simple concepts in Django to help you get started:
1. What is Django?
Django is a high-level Python web framework that promotes rapid development and clean,
pragmatic design. It follows the Model-View-Template (MVT) architecture, which is similar to the
MVC (Model-View-Controller) pattern.
2. Django Project and App Structure
fi
• Project: A Django project is a collection of settings and con gurations for an entire website.
You can create a project using the following command:
bash
Copy code
django-admin startproject project_name
• App: A Django app is a web application that performs a speci c task within a project. You
can create an app using:
bash
Copy code
python manage.py startapp app_name
• A project can contain multiple apps.
3. Views
Views handle the logic behind web pages and send back responses (HTML, JSON, etc.). A basic
view looks like this:
python
Copy code
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!")
To map this view to a URL, modify urls.py:
python
Copy code
from django.urls import path
from .views import home
urlpatterns = [
path('', home),
]
4. Templates
fi
fi
Templates de ne the HTML structure of your web pages. You can pass data from views to
templates. First, create a templates directory and an HTML le:
<!-- templates/home.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home Page</title>
</head>
<body>
<h1>Welcome to Django!</h1>
<p>{{ message }}</p>
</body>
</html>
In your view, pass a context to the template:
from django.shortcuts import render
def home(request):
context = {'message': 'This is a Django tutorial'}
return render(request, 'home.html', context)
fi
fi