Skip to content

Latest commit

Β 

History

History
182 lines (146 loc) Β· 4.54 KB

README.md

File metadata and controls

182 lines (146 loc) Β· 4.54 KB

πŸš€ Django Project

πŸ“Œ Project Overview

This is a Django-based web application that demonstrates fundamental concepts such as class-based views (CBV), URL routing, templates, and testing.

πŸ“œ Table of Contents


πŸ›  Installation

To set up this project, follow these steps:

  1. Clone the Repository

    git clone https://github.com/Amin-moniry-pr7/DJANGO_CODES.git
    cd DJANGO_CODES
  2. Create and Activate Virtual Environment

    • Windows:
      python -m venv .amin
      .amin\Scripts\Activate
    • Mac/Linux:
      python3 -m venv .amin
      source .amin/bin/activate
  3. Install Dependencies

    pip install -r requirements.txt
  4. Run Migrations

    python manage.py migrate
  5. Start the Development Server

    python manage.py runserver

πŸ“‚ Project Structure

Django_Project/
│── πŸ“ Amin_message/       # Django App
β”‚   β”œβ”€β”€ πŸ“ migrations/     # Database Migrations
β”‚   β”œβ”€β”€ πŸ“ templates/      # HTML Templates
β”‚   β”œβ”€β”€ πŸ“œ urls.py         # URL Routing
β”‚   β”œβ”€β”€ πŸ“œ views.py        # View Functions
│── πŸ“ config_amin/        # Django Project Config
β”‚   β”œβ”€β”€ βš™ settings.py      # Project Settings
β”‚   β”œβ”€β”€ πŸ“œ urls.py         # Main URL Config
│── πŸ“ templates/          # Global Templates
│── πŸ”§ manage.py           # Django Management Script

πŸ“Œ Usage

Once the server is running, open your browser and visit:

http://127.0.0.1:8000/

You should see a Welcome Message rendered from the home.html template.


✨ Features

βœ… Class-Based Views (CBV) for better structure. βœ… Template Rendering to dynamically display content. βœ… URL Routing for navigation. βœ… Unit Testing to ensure application stability.


πŸ“– Code Explanation

πŸ“ 1. Views (views.py)

This project uses Class-Based Views (CBV) instead of Function-Based Views (FBV) for better scalability:

from django.views.generic import TemplateView

class MessageView(TemplateView):
    template_name = 'home.html'

πŸ”— 2. URL Routing (urls.py)

The URL configuration routes the base URL to the MessageView:

from django.urls import path
from .views import MessageView

urlpatterns = [
    path('', MessageView.as_view(), name='Amin_message'),
]

🎨 3. Templates (home.html)

The template extends a base layout and renders a Welcome Message:

{% extends 'base.html' %}

{% block content %}
    <h1><b><i>πŸŽ‰ WELCOME TO OUR SITE πŸŽ‰</i></b></h1>
    <small>In this site, you can learn <strong>HTML & CSS</strong>.</small>
{% endblock content %}

πŸ§ͺ 4. Testing (tests.py)

Unit test to check if the correct template is rendered:

from django.test import TestCase
from django.urls import reverse

class TemplateTestCase(TestCase):
    def test_contains_correct_template(self):
        response = self.client.get(reverse('Amin_message'))
        self.assertContains(response, '<h1><b><i>πŸŽ‰ WELCOME TO OUR SITE πŸŽ‰</i></b></h1>')

πŸ”§ 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

βš™ Django Commands

  • Check Django version:
    python -m django --version
  • Create a new Django project:
    python -m django startproject config_amin
  • Run the development server:
    python manage.py runserver

πŸ†š Function-Based Views (FBV) vs. Class-Based Views (CBV)

  • FBV (Function-Based Views) are simple and easy to understand but can become difficult to manage in large applications.
  • CBV (Class-Based Views) provide better modularity and reusability.
  • In this project, CBV is used, but in some cases, FBV might be better for simple views.

πŸ“œ 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.

✨ Happy Coding! πŸš€πŸŽ‰