Tuesday, April 22, 2025

Django Files, Project Structure


C:\my_projects>python manage.py startapp my_site

C:\my_projects>

Running the python manage.py startapp my_site command in the my_projects directory creates a new Django application named my_site.

This command creates a new directory called my_site, which contains several files and folders that make up the new Django application. These files include a models.py file for defining data models, a views.py file for handling HTTP requests and returning responses, a urls.py file for mapping URLs to views, and many other files and folders that are used to manage the application.

Once this command is executed, the my_site application is ready to be developed and integrated into the larger Django project. 


C:\my_projects>dir
 Volume in drive C is New Volume
 
 Directory of C:\my_projects

07/26/2020  10:18 AM    <DIR>          .
07/26/2020  10:18 AM    <DIR>          ..
07/26/2020  10:09 AM               652 manage.py
07/26/2020  10:18 AM    <DIR>          my_projects
07/26/2020  10:18 AM    <DIR>          my_site
               1 File(s)            652 bytes
               4 Dir(s)  77,952,106,496 bytes free

C:\my_projects>

This output indicates that the my_site app has been successfully created using the python manage.py startapp my_site command. 


C:\my_projects>cd my_site

C:\my_projects\my_site>dir
 Volume in drive C is New Volume

 Directory of C:\my_projects\my_site

07/26/2020  10:18 AM    <DIR>          .
07/26/2020  10:18 AM    <DIR>          ..
07/26/2020  10:18 AM                66 admin.py
07/26/2020  10:18 AM                93 apps.py
07/26/2020  10:18 AM    <DIR>          migrations
07/26/2020  10:18 AM                60 models.py
07/26/2020  10:18 AM                63 tests.py
07/26/2020  10:18 AM                66 views.py
07/26/2020  10:18 AM                 0 __init__.py
               6 File(s)            348 bytes
               3 Dir(s)  77,951,762,432 bytes free

C:\my_projects\my_site> 

Create file urls.py in this level (same level where we have views.py)

In Django, the urls.py file is used to map the URL patterns to their corresponding views. It acts as a routing mechanism for incoming requests to the web application.

When a user makes a request to a specific URL, Django checks the URL patterns defined in the urls.py file to find a matching view function. If a match is found, Django calls the corresponding view function to generate the HTTP response that will be sent back to the user.

Without the urls.py file, Django would not know how to route requests to the appropriate views, and the web application would not be able to function properly.

Django migrations are a way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. Migrations are Django's way of managing and applying changes to the database schema over time, as you evolve your application.

When you make changes to your models, you can create a migration that specifies the changes, and then apply that migration to your database to make the changes to the schema. This helps you keep your database schema in sync with your models and makes it easier to manage changes to your application over time.

Go to C:\my_projects\my_projects and find settings.py. Open it and add 'my_site' at the bottom of INSTALLED APPS:


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'my_site',
]

Installed apps in Django are the components or modules that are included in the project to provide additional functionality. These apps can be either built-in apps provided by Django itself or third-party apps developed by other developers.

By default, when a new Django project is created, several built-in apps are automatically included in the project's settings.py file, such as:

  • django.contrib.admin: provides a powerful web-based admin interface for managing the project's data.
  • django.contrib.auth: provides user authentication functionality.
  • django.contrib.contenttypes: provides generic relations between models.
  • django.contrib.sessions: provides session management functionality.
  • django.contrib.messages: provides message passing functionality.
  • django.contrib.staticfiles: provides a simple way to manage static files such as images, CSS, and JavaScript.

However, developers can install additional third-party apps from the Python Package Index (PyPI) or develop their own custom apps for their specific needs. These apps can be added to the INSTALLED_APPS list in the project's settings.py file to include them in the project.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...