manage.py: Command-line utility for managing the project.settings.py: Configuration file for the project.urls.py: Maps URLs to views.models.py: Defines the database schema.views.py: Contains logic for handling requests.templates/: Stores HTML templates.wsgi.py: Web server getway interfaceasgi.py: Asynchronous server getway interfacefrom django.views.generic.base import TemplateView
class HomePageView(TemplateView):
template_name = "home.html"from django.views.generic.detail import DetailView
from .models import Book
class BookDetailView(DetailView):
model = Book
template_name = "book_detail.html"from django.views.generic.list import ListView
from .models import Book
class BookListView(ListView):
model = Book template_name = "book_list.html"
context_object_name = "books" # Default is 'object_list'from django.views.generic.edit import FormView
from .forms import ContactForm
class ContactFormView(FormView):
template_name = "contact.html"
form_class = ContactForm
success_url = "/thanks/"
def form_valid(self, form):
# Process the form (e.g., send email)
return super().form_valid(form)from django.views.generic.edit import CreateView
from .models import Book
class BookCreateView(CreateView):
model = Book
fields = ['title', 'author', 'price']
template_name = "book_form.html"
success_url = "/books/"from django.views.generic.edit import UpdateView
class BookUpdateView(UpdateView):
model = Book
fields = ['title', 'author', 'price']
template_name = "book_form.html"
success_url = "/books/"from django.views.generic.edit import DeleteView
class BookDeleteView(DeleteView):
model = Book
template_name = "book_confirm_delete.html"
success_url = "/books/"django.forms.Formt used for creating non-model-based forms.django.formsModelForm, used for creating forms tied to database models.forms.Form or forms.ModelForm.form.is_valid())from django import forms from django.shortcuts import render
class ContactForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField()
def contact_view(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
return HttpResponse("Form submitted successfully!")
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})FileFieId or ImageFieId to your model.MEDIA_URL and MEDIA_ROOT in settings.pyenctype="multipart/form-data" to the form.request.FILES to access uploaded files.from django.shortcuts import render
def upload_file(request):
if request.method == 'POST':
uploaded_file = request.FILES['file']
with open(f'media/{uploaded_file.name}', 'wb+') as destination:
for chunk in uploaded_file.chunks():
destination.write(chunk)
return render(request, 'upload.html'){{ }} for variable interpolation.{% %} for logic like loops or conditionals.<!-- templates/home.html -->
<!DOCTYPE html>
<html>
<body>
<h1>Welcome, {{ user_name }}</h1>
</body>
</html>redirect() function to send users to another URL.from django.shortcuts import redirect
def redirect_view(request):
return redirect('/success/')urls.py.from django.urls import path
from . import views
urlpatterns = [
path( '', views.home_view, name='home'),
path('about/', views.about_view, name='about'),
]ReportLab using pip install reportlab.from django.http import HttpResponse
from reportlab.pdfgen import canvas
def generate_pdf(request):
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="example.pdf"'
p = canvas.Canvas(response)
p.drawString(100, 750, "Hello, ReportLab!")
p.showPage()
p.save()
return responseMade By SOU Student for SOU Students