This is a Django-based web application that demonstrates fundamental concepts such as class-based views (CBV), URL routing, templates, and testing.
- π Installation
- π Project Structure
- π Usage
- β¨ Features
- π Code Explanation
- π§ Git Commands
- β Django Commands
- π License
To set up this project, follow these steps:
-
Clone the Repository
git clone https://github.com/Amin-moniry-pr7/DJANGO_CODES.git cd DJANGO_CODES
-
Create and Activate Virtual Environment
- Windows:
python -m venv .amin .amin\Scripts\Activate
- Mac/Linux:
python3 -m venv .amin source .amin/bin/activate
- Windows:
-
Install Dependencies
pip install -r requirements.txt
-
Run Migrations
python manage.py migrate
-
Start the Development Server
python manage.py runserver
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
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.
β Class-Based Views (CBV) for better structure. β Template Rendering to dynamically display content. β URL Routing for navigation. β Unit Testing to ensure application stability.
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'
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'),
]
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 %}
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>')
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
- 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
- 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.
π 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! ππ