Understanding Python Files in a Django App – Full Guide for Beginners

bmpokhrel9 | May 5, 2025


Understanding Python Files in a Django App – Full Guide for Beginners

Understanding Python Files in a Django App – Full Guide for Beginners

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. When you create a Django app, it comes with a set of default Python files. Understanding the purpose of each of these files is essential for working efficiently in Django.

In this article, we’ll break down the most important .py (Python) files that are automatically generated—or commonly added—in a Django app folder, such as myapp/.

 

 

📁 Django App Structure Overview

When you create an app using the command:

python manage.py startapp myapp

Django generates the following folder structure:

myapp/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── views.py
├── migrations/

Let’s explore what each of these Python files does:


1. __init__.py

  • Purpose: This file tells Python that this directory should be treated as a package.
  • Details: It’s usually empty but necessary for imports to work correctly.

✅ You don’t usually need to modify this.


2. admin.py

  • Purpose: This file is used to register your models so they appear in Django’s admin panel.
  • Example:
from django.contrib import admin
from .models import BlogPost

admin.site.register(BlogPost)

✅ Tip: Register every model you want to manage from the Django admin dashboard here.


3. apps.py

  • Purpose: Contains the configuration for the app.
  • Auto-generated when you create the app.
from django.apps import AppConfig

class MyappConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'myapp'

✅ You rarely need to change this unless you're doing something advanced like app-specific signals.


4. models.py

  • Purpose: This is where you define your data models (i.e., database tables).
  • Example:
from django.db import models

class BlogPost(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

✅ After creating models, run:

python manage.py makemigrations
python manage.py migrate

5. views.py

  • Purpose: Contains the logic for handling requests and returning responses (i.e., your website pages or API endpoints).
  • Example:
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, world! This is my app.")

✅ Views can return HTML templates, JSON responses, or raw text.


6. tests.py

  • Purpose: For writing unit tests for your app.
  • Example:
from django.test import TestCase

class SimpleTest(TestCase):
    def test_addition(self):
        self.assertEqual(1 + 1, 2)

✅ Use this for writing automated tests as your project grows.


7. migrations/ folder

  • Purpose: Stores migration files that track changes in your database schema over time.
  • Generated By: makemigrations command.

✅ You usually don’t modify these files manually.


8. (Optional) urls.py – You Should Add This

This file is not created by default in a new app, but it's best practice to create it so your app has its own URL routing.

Example:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

Then include it in your project’s main urls.py:

from django.urls import include, path

urlpatterns = [
    path('', include('myapp.urls')),
]

✅ Summary Table

File NamePurpose
__init__.pyMakes the directory a Python package
admin.pyRegister models for Django admin panel
apps.pyApp configuration and metadata
models.pyDatabase models (tables)
views.pyRequest/response logic (views)
tests.pyUnit tests
urls.pyURL routing for the app (you add this)
migrations/Database migration files

🚀 Final Thoughts

Understanding the purpose of each Python file in a Django app helps you organize your code better and makes it easier to scale and maintain your project. As you build larger applications, keeping a clean and structured app layout becomes even more critical.

If you're new to Django, don’t worry about mastering everything at once. Start small, get familiar with each file, and build from there!

 




0 COMMENTS:

Create Apache Configuration for Your Django wsgi Project

2025-06-07 03:59:24.874882+00:00

Read More
How to Create a Django App – Step by Step Guide

2025-06-07 03:59:24.874882+00:00

Read More
Understanding Python Files in a Django App – Full Guide for Beginners

2025-06-07 03:59:24.874882+00:00

Read More
Understanding Django Settings: The Backbone of Your Web App Configuration

2025-06-07 03:59:24.874882+00:00

Read More
How to Use Django collectstatic the Right Way

2025-06-07 03:59:24.874882+00:00

Read More