Skip to content

πŸš€ Django Project – In this project, you will learn Class-Based Views (CBV), URL routing, template rendering, and unit testing in Django. It helps you understand how to structure a scalable web application. You’ll see how views and templates interact dynamically. Additionally, you’ll practice writing tests to ensure application stability.

License

Notifications You must be signed in to change notification settings

Amin-moniry-pr7/DJANGO_PRACTICE_CODE

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ DJANGO_PRACTICE_TEST

πŸ“Œ Project Overview

This project is a simple Django application designed to practice and test basic Django functionalities. It includes:

  • Model-based List View: Displays a list of messages stored in the database.
  • Templates: Custom templates (F_D.html, S_D.html) for rendering views and creating dynamic content.
  • Admin Panel: Allows you to manage data in the Django Admin interface.

πŸ“‹ Prerequisites

  1. 🐍 Python 3.x
  2. 🌐 Django 5.x
  3. πŸ—ƒ SQLite (default database)

πŸ“‚ Project Structure

DJANGO_PRACTICE_TEST/
β”‚
β”œβ”€β”€ πŸ“œ manage.py                # Django management script
β”œβ”€β”€ πŸ“š db.sqlite3               # SQLite database file
β”œβ”€β”€ 🌱 .env                     # Environment variables for project settings
β”œβ”€β”€ πŸ’» PM_A/                    # Custom Django app
β”‚   β”œβ”€β”€ πŸ“¦ migrations/
β”‚   β”œβ”€β”€ πŸ—‚ models.py            # Model definitions
β”‚   β”œβ”€β”€ 🌐 views.py             # Views handling
β”‚   β”œβ”€β”€ πŸ”— urls.py              # URL routing for PM_A app
β”‚   β”œβ”€β”€ πŸ–₯ templates/           # Template files (F_D.html, S_D.html)
β”‚   β”œβ”€β”€ πŸ—‚ admin.py             # Register models with admin site
β”‚   └── πŸ“‘ tests.py             # Test cases for views and responses
└── πŸ— django_AM/
    β”œβ”€β”€ βš™ settings.py          # Main project settings
    β”œβ”€β”€ πŸ”— urls.py              # Global URL routing
    └── πŸ–₯ wsgi.py              # WSGI configuration for the project

πŸ“ Code Explanation

1. views.py

In views.py, you define the class-based view PMRESPONDE, which inherits from ListView. This view will display a list of items from your ab model. The ListView automatically handles rendering the list, while the template (S_D.html) displays the content.

from django.views.generic import ListView
from .models import ab

class PMRESPONDE(ListView):
    model = ab
    template_name = 'S_D.html'  # This is the template that will be used to render the list of items.

2. models.py

In models.py, you've defined a simple model ab with a single field text. This model will store messages that are shown in the ListView.

from django.db import models

class ab(models.Model):
    text = models.CharField(max_length=200)

    def __str__(self):
        return self.text  # This will display the text when rendering the list of items in the admin panel.

3. urls.py

In urls.py, you map the root URL ('') to the PMRESPONDE class-based view. This ensures that when you visit the homepage of the app, the list of messages is displayed.

from django.urls import path
from .views import PMRESPONDE

urlpatterns = [
    path('', PMRESPONDE.as_view(), name='PM_A'),  # Maps the root URL to the PMRESPONDE view.
]

4. templates (F_D.html & S_D.html)

In your templates, you have F_D.html and S_D.html.

  • F_D.html is likely a base template that other templates inherit from (this is indicated by the {% extends 'F_D.html' %} line in the S_D.html template).
  • S_D.html is used to render the list of messages from the ab model.

For S_D.html, here's how you render the list:

{% extends 'F_D.html' %}

{% block content %}
    <h1><b>!USERS MESSAGES!</b></h1>

    <ul>
        {% for ab_item in object_list %}
            <li>{{ ab_item.text }}</li>  <!-- Displays each message stored in the ab model -->
        {% endfor %}
    </ul>
{% endblock %}

5. admin.py

You register the ab model in admin.py to allow it to be managed through the Django Admin interface.

from django.contrib import admin
from .models import ab

admin.site.register(ab)  # Registers the ab model to appear in the Django Admin panel.

6. tests.py

In tests.py, you have test cases that check if the view correctly renders the template and returns a 200 status code:

from django.test import SimpleTestCase
from django.urls import reverse

class TestViews(SimpleTestCase):
    def test_1(self):
        response = self.client.get('/PM_A/')
        self.assertEqual(response.status_code, 200)  # Checks if the status code is 200, meaning the page loaded correctly.

    def test_2(self):
        response = self.client.get(reverse('PM_A'))  # Uses the URL name to generate the URL.
        self.assertEqual(response.status_code, 200)

    def test_3(self):
        response = self.client.get(reverse('PM_A'))
        self.assertTemplateUsed(response, 'S_D.html')  # Verifies that the correct template is used.

πŸ”§ Running the Application

To run your Django application, make sure you have completed the installation and setup steps, including creating a superuser if you want to access the Admin interface.

  1. Start the Development Server

    python manage.py runserver

    This starts the Django development server. By default, the server will run on http://127.0.0.1:8000/.

  2. Access the Admin Panel

    • To access the Django Admin interface, visit http://127.0.0.1:8000/admin/ and log in with the superuser credentials you created earlier.
  3. Visit the Application's Home Page

    • After starting the server, navigate to http://127.0.0.1:8000/ in your browser to view the list of messages from the ab model, rendered using the S_D.html template.

πŸ›  Installation

  1. Clone this repository:

    git clone https://github.com/Amin-moniry-pr7/DJANGO_PRACTICE_CODE.git
  2. Install required dependencies:

    pip install -r requirements.txt
  3. Set up your environment variables:

    • Create a .env file in the root directory and configure your settings like the secret key.
  4. Apply migrations:

    python manage.py migrate
  5. Create a superuser to access the Django Admin:

    python manage.py createsuperuser

πŸ”§ Git Commands

To set up a Git repository and push changes:

πŸš€ git init
πŸ“Œ git add *
πŸ“ git commit -m "Django_Project"
πŸ”— git remote add amin "repository_address"
⬆ git push amin master

πŸ“œ License

πŸ”– This project is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License.

Copyright (c) 2025 Amin Moniry
You are free to use and modify this code for non-commercial purposes.

πŸ“ GitHub Repository

You can find the full code of the project on GitHub at the following link:

https://github.com/Amin-moniry-pr7

About

πŸš€ Django Project – In this project, you will learn Class-Based Views (CBV), URL routing, template rendering, and unit testing in Django. It helps you understand how to structure a scalable web application. You’ll see how views and templates interact dynamically. Additionally, you’ll practice writing tests to ensure application stability.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages