Django Project Structure: A Layer-Oriented Cheat Sheet

  1. models.py
    • Purpose: Defines the data structure. This file is used to create database schema (tables, fields, and relationships).
    • Contains: Classes that extend Django's models.Model, field definitions, and metadata.
    • Example:
      
      from django.db import models
      
      class MyModel(models.Model):
          my_field = models.CharField(max_length=100)
      
      
  2. views.py
    • Purpose: Contains the logic and control flow for handling requests and defines HTTP responses.
    • Contains: Functions or classes (class-based views) that take a web request and return a web response.
    • Example:
      
      from django.http import HttpResponse
      from django.views import View
      from django.views.generic import TemplateView
      
      def my_view(request):
          return HttpResponse("Hello, World!")
      
      class MyView(View):
          def get(self, request):
              return HttpResponse("Hello, World from Class-Based View!")
      
      class MyTemplateView(TemplateView):
          template_name = "base/home.html"
      
          def get_context_data(self, **kwargs):
              context = super().get_context_data(**kwargs)
              context['custom_data'] = "This is custom data"
              return context
                  
                  
  3. urls.py
    • Purpose: Defines URL patterns (URLconf) to route requests to the appropriate views.
    • Contains: URL patterns that are matched with view functions or classes.
    • Example:
      
      from django.urls import path
      from . import views
      
      urlpatterns = [
          path('hello-func/', views.my_view, name='my-func-view'),
          path('hello-class/', views.MyView.as_view(), name='my-class-view'),
          path('', views.MyTemplateView.as_view(), name='home'),
      ]
                  
                  
  4. templates/
    • Purpose: Contains HTML files that define the structure and layout of web pages (the frontend).
    • Contains: HTML files with Django Template Language (DTL) for dynamic content rendering.
    • Example:
      
      <html>
      <head>
          <title>My Page</title>
      </head>
      <body>
          <h1>{{ page_title }}</h1>
      </body>
      </html>
      
      
  5. admin.py
    • Purpose: Registers models to include them in the Django admin site for CRUD operations.
    • Contains: Code to customize how models are displayed and interacted within the admin interface.
    • Example:
      
      from django.contrib import admin
      from .models import MyModel
      
      admin.site.register(MyModel)
      
      
  6. forms.py (Optional)
    • Purpose: Defines forms for user input, usually tied to models.
    • Contains: Form classes that define fields and validation behaviors.
    • Example:
      
      from django import forms
      from .models import MyModel
      
      class MyForm(forms.ModelForm):
          class Meta:
              model = MyModel
              fields = ['my_field']
      
      
  7. tests.py
    • Purpose: Contains test cases to test your app's functionality.
    • Contains: Test functions or classes to check for the correct behavior of your views, models, and other parts of the application.
    • Example:
      
      from django.test import TestCase
      from .models import MyModel
      
      class MyModelTests(TestCase):
          def test_str_representation(self):
              entry = MyModel(my_field="Test")
              self.assertEqual(str(entry), entry.my_field)
      
      
  8. static/ (Optional)
    • Purpose: Stores static files like CSS, JavaScript, and images.
    • Contains: Subdirectories for different types of static files.
    • Example:
      
      /static/
          /css/
          /js/
          /images/
      
      
  9. migrations/
    • Purpose: Contains migration files that Django uses to evolve the database schema over time.
    • Contains: Automatically generated files that represent the changes in your models.
    • Example:
      
      /migrations/
          0001_initial.py
          0002_auto_20230101_1234.py