├── README.md ├── wardData.sql └── ward_project ├── manage.py ├── residents ├── __init__.py ├── admin.py ├── apps.py ├── models.py ├── templates │ ├── 404.html │ ├── base.html │ ├── dashboard.html │ ├── edit_resident.html │ ├── edit_resident_error.html │ ├── homee.html │ ├── image │ │ └── engineer-svgrepo-com.svg │ ├── login.html │ ├── residents │ │ ├── detail.html │ │ └── search.html │ └── signup.html ├── tests.py └── views.py └── ward_project ├── __init__.py ├── __pycache__ ├── __init__.cpython-311.pyc ├── settings.cpython-311.pyc ├── urls.cpython-311.pyc └── wsgi.cpython-311.pyc ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py /README.md: -------------------------------------------------------------------------------- 1 | # Ward Management System 2 | 3 | A simple Django-based web application for managing residents. This project allows users to add, search, view details, and delete residents. 4 | 5 | ## Features 6 | - Add a new resident 7 | - Search for residents by name, occupation, business, or house number 8 | - View resident details 9 | - Delete residents 10 | 11 | ## DataBase 12 | - create a database using the the wardData.sql 13 | - change your username and password in settings.py 14 | - to login as a user use default password as password123 and the respective users name 15 | - Delete residents 16 | 17 | ## Prerequisites 18 | ### 1. Check if Python is Installed 19 | Run the following command to check if Python is installed: 20 | ```bash 21 | python --version 22 | ``` 23 | If Python is not installed, download and install it from [Python's official website](https://www.python.org/downloads/). 24 | 25 | ### 2. Check if Django is Installed 26 | Run: 27 | ```bash 28 | python -m django --version 29 | ``` 30 | If Django is not installed, install it using: 31 | ```bash 32 | pip install django 33 | ``` 34 | 35 | ## Installation and Setup 36 | 37 | ### 1. Clone the Repository 38 | ```bash 39 | git clone https://github.com/KaranRajiwade/jangoBasicProject.git 40 | cd ward_project 41 | ``` 42 | 43 | ### 2. Create a Virtual Environment 44 | ```bash 45 | python -m venv venv 46 | ``` 47 | Activate the virtual environment: 48 | - On Windows: 49 | ```bash 50 | venv\Scripts\activate 51 | ``` 52 | - On macOS/Linux: 53 | ```bash 54 | source venv/bin/activate 55 | ``` 56 | 57 | 58 | ### 4. Set Up the Database 59 | ```bash 60 | python manage.py migrate 61 | ``` 62 | 63 | ### 5. Create a Superuser (Optional, for Admin Panel) 64 | ```bash 65 | python manage.py createsuperuser 66 | ``` 67 | Follow the prompts to create a username and password. 68 | 69 | ### To install mysqlclient, run: 70 | ```bash 71 | pip install mysqlclient 72 | ``` 73 | 83 | 84 | 85 | 86 | 87 | ### 6. Run the Development Server 88 | ```bash 89 | python manage.py runserver 90 | ``` 91 | 92 | ### 7. Open the Application 93 | Visit: [http://127.0.0.1:8000/](http://127.0.0.1:8000/) in your browser. 94 | 95 | ## Project Structure 96 | ``` 97 | ward_project/ 98 | ├── residents/ # Django app for residents 99 | │ ├── migrations/ # Database migrations 100 | │ ├── templates/ # HTML Templates 101 | │ ├── views.py # Application logic 102 | │ ├── models.py # Database models 103 | │ ├── urls.py # URL routing 104 | │ 105 | ├── ward_project/ # Main Django project settings 106 | │ ├── settings.py # Configuration file 107 | │ ├── urls.py # Main URL dispatcher 108 | │ 109 | ├── Mysql # Mysql database file 110 | ├── manage.py # Django management script 111 | ├── requirements.txt # Dependencies file 112 | ``` 113 | 114 | ## How the Project Works 115 | ### 1. Searching for Residents 116 | - Users can search for residents by name, occupation, business, or house number. 117 | - The search query filters the resident database and displays matching results. 118 | 119 | ### 2. Viewing Resident Details 120 | - Clicking on a resident's name will show detailed information about them. 121 | 122 | ### 3. Adding a Resident 123 | - Users can add a new resident using the `add_resident.html` form. 124 | - After submission, the resident’s data is stored in the database. 125 | 126 | ### 4. Deleting a Resident 127 | - Users can delete a resident, which removes them from the database. 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /wardData.sql: -------------------------------------------------------------------------------- 1 | -- use this to create database 2 | CREATE DATABASE ward; 3 | 4 | -- this will use created database 5 | USE ward; 6 | 7 | 8 | CREATE TABLE residents ( 9 | id INT AUTO_INCREMENT PRIMARY KEY, 10 | name VARCHAR(100), 11 | age INT, 12 | house_no VARCHAR(10), 13 | occupation VARCHAR(50), 14 | business VARCHAR(50), 15 | no_of_children INT DEFAULT 0 16 | ); 17 | 18 | INSERT INTO residents (name, age, house_no, occupation, business, no_of_children) VALUES 19 | ('Aarav Sharma', 35, 'H101', 'Teacher', 'None', 2), 20 | ('Kavya Gupta', 28, 'H102', 'Engineer', 'Freelancer', 0), 21 | ('Rohan Iyer', 42, 'H103', 'Doctor', 'Clinic', 3), 22 | ('Priya Desai', 30, 'H104', 'Designer', 'Boutique', 0), 23 | ('Amit Kumar', 45, 'H105', 'Businessman', 'Retail', 4), 24 | ('Sanya Singh', 33, 'H106', 'Lawyer', 'None', 0), 25 | ('Anjali Verma', 27, 'H107', 'Software Developer', 'Startup', 0), 26 | ('Rajesh Patel', 50, 'H108', 'Farmer', 'Dairy', 5), 27 | ('Meera Joshi', 29, 'H109', 'Artist', 'Gallery', 0), 28 | ('Vikram Chauhan', 38, 'H110', 'Police Officer', 'None', 2), 29 | ('Deepika Reddy', 41, 'H111', 'Chef', 'Restaurant', 3), 30 | ('Aditya Nair', 26, 'H112', 'Student', 'None', 0), 31 | ('Neha Mehta', 34, 'H113', 'HR Manager', 'None', 1), 32 | ('Siddharth Malhotra', 37, 'H114', 'Scientist', 'None', 0), 33 | ('Pooja Yadav', 31, 'H115', 'Writer', 'Publishing', 2), 34 | ('Karan Kapoor', 39, 'H116', 'Photographer', 'Studio', 3), 35 | ('Isha Jain', 25, 'H117', 'Researcher', 'None', 0), 36 | ('Ravi Thakur', 44, 'H118', 'Architect', 'Construction', 4), 37 | ('Simran Gill', 28, 'H119', 'Banker', 'None', 0), 38 | ('Arjun Das', 46, 'H120', 'Shopkeeper', 'Retail', 3); 39 | 40 | 41 | 42 | SELECT * FROM residents; 43 | 44 | -- INSERT INTO residents (name, age, house_no, occupation, business, no_of_children) VALUES 45 | -- ('Aarav Sharma', 35, 'H101', 'Teacher', 'None', 2), 46 | -- ('Kavya Gupta', 28, 'H102', 'Engineer', 'Freelancer', 0); 47 | 48 | 49 | -- SELECT name, house_no, COUNT(*) 50 | -- FROM residents 51 | -- GROUP BY name, house_no 52 | -- HAVING COUNT(*) > 1; 53 | 54 | -- DELETE r1 55 | -- FROM residents r1 56 | -- JOIN residents r2 57 | -- ON r1.name = r2.name 58 | -- AND r1.house_no = r2.house_no 59 | -- AND r1.id > r2.id; 60 | 61 | -- for adding users use the following commands(for the login purpose): 62 | -- run this after running migration and after running server (this will insert data in auth_user) 63 | 64 | INSERT INTO auth_user (username, password, first_name, last_name, email, is_staff, is_active, is_superuser, date_joined) 65 | VALUES 66 | ('Aarav Sharma', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Aarav', 'Sharma', 'aarav@example.com', 0, 1, 0, NOW()), 67 | ('Kavya Gupta', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Kavya', 'Gupta', 'kavya@example.com', 0, 1, 0, NOW()), 68 | ('Rohan Iyer', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Rohan', 'Iyer', 'rohan@example.com', 0, 1, 0, NOW()), 69 | ('Priya Desai', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Priya', 'Desai', 'priya@example.com', 0, 1, 0, NOW()), 70 | ('Amit Kumar', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Amit', 'Kumar', 'amit@example.com', 0, 1, 0, NOW()), 71 | ('Sanya Singh', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Sanya', 'Singh', 'sanya@example.com', 0, 1, 0, NOW()), 72 | ('Anjali Verma', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Anjali', 'Verma', 'anjali@example.com', 0, 1, 0, NOW()), 73 | ('Rajesh Patel', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Rajesh', 'Patel', 'rajesh@example.com', 0, 1, 0, NOW()), 74 | ('Meera Joshi', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Meera', 'Joshi', 'meera@example.com', 0, 1, 0, NOW()), 75 | ('Vikram Chauhan', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Vikram', 'Chauhan', 'vikram@example.com', 0, 1, 0, NOW()), 76 | ('Deepika Reddy', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Deepika', 'Reddy', 'deepika@example.com', 0, 1, 0, NOW()), 77 | ('Aditya Nair', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Aditya', 'Nair', 'aditya@example.com', 0, 1, 0, NOW()), 78 | ('Neha Mehta', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Neha', 'Mehta', 'neha@example.com', 0, 1, 0, NOW()), 79 | ('Pooja Yadav', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Pooja', 'Yadav', 'pooja@example.com', 0, 1, 0, NOW()), 80 | ('Karan Kapoor', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Karan', 'Kapoor', 'karan@example.com', 0, 1, 0, NOW()), 81 | ('Isha Jain', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Isha', 'Jain', 'isha@example.com', 0, 1, 0, NOW()), 82 | ('Ravi Thakur', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Ravi', 'Thakur', 'ravi@example.com', 0, 1, 0, NOW()), 83 | ('Simran Gill', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Simran', 'Gill', 'simran@example.com', 0, 1, 0, NOW()), 84 | ('Karan Rajiwade', 'pbkdf2_sha256$870000$u7hAKuIZrCxDjIvsHa8bpq$3ehVmELniSBJAHor7SZ87qdA5WOGk/EsL03EwRprA3k=', 'Karan', 'Rajiwade', 'karanraj@example.com', 0, 1, 0, NOW()); 85 | 86 | UPDATE residents 87 | JOIN auth_user ON residents.name = auth_user.username 88 | SET residents.user_id = auth_user.id 89 | WHERE residents.id > 0; -------------------------------------------------------------------------------- /ward_project/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ward_project.settings") 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == "__main__": 22 | main() 23 | -------------------------------------------------------------------------------- /ward_project/residents/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KaranRajiwade/jangoBasicProject/f3391e8ec6c7b6ec95b54b402a7d2d7f4a92c8ba/ward_project/residents/__init__.py -------------------------------------------------------------------------------- /ward_project/residents/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /ward_project/residents/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ResidentsConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "residents" 7 | -------------------------------------------------------------------------------- /ward_project/residents/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from django.contrib.auth.models import User 3 | 4 | # Create your models here. 5 | 6 | # class Resident(models.Model): 7 | # name = models.CharField(max_length=100) 8 | # age = models.IntegerField() 9 | # house_no = models.CharField(max_length=10) 10 | # occupation = models.CharField(max_length=50) 11 | # business = models.CharField(max_length=50, blank=True, null=True) 12 | # no_of_children = models.IntegerField(default=0) 13 | 14 | # def __str__(self): 15 | # return self.name 16 | #in the example it uses a database which is not predefined it we have to create a table and insert values in it in the same db as we selected 17 | 18 | #updated one which uses predefined db which is "ward" 19 | # this is second step creating data-model for selected DB 20 | # next step is displaying data in the webpage 21 | # A view fetches data from the database, and a template displays it. 22 | 23 | class Resident(models.Model): 24 | user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True) # Links Resident to User 25 | name = models.CharField(max_length=100) 26 | age = models.IntegerField() 27 | house_no = models.CharField(max_length=10) 28 | occupation = models.CharField(max_length=50) 29 | business = models.CharField(max_length=50, blank=True, null=True) 30 | no_of_children = models.IntegerField(default=0) 31 | 32 | class Meta: 33 | db_table = 'residents' # Ensures it uses the existing MySQL table # Use the original table 34 | 35 | def __str__(self): 36 | return self.name -------------------------------------------------------------------------------- /ward_project/residents/templates/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Page Not Found 7 | 17 | 18 | 19 |

Oops! Page Not Found

20 | 21 | {% load static %} 22 | engineer 23 | 24 |

The page you are looking for does not exist.

25 | Go to Login 26 | 27 | 28 | -------------------------------------------------------------------------------- /ward_project/residents/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | {% block title %}My Website{% endblock %} 7 | 8 | 192 | 193 | 194 | 195 | 196 | 213 | 214 | 215 |
216 | {% block content %} 217 | {% endblock %} 218 |
219 | 220 | 227 | 228 | 229 | 230 | -------------------------------------------------------------------------------- /ward_project/residents/templates/dashboard.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block title %}Dashboard - My Website{% endblock %} 4 | 5 | {% block content %} 6 |
7 |

Welcome, {{ user.username }}!

8 | 9 | {% if user.is_staff %} 10 |

All Residents

11 | {% else %} 12 |

Your Residents

13 | {% endif %} 14 | 15 | 16 |
17 | 19 | 22 |
23 | 24 | 37 |
38 | {% endblock %} 39 | -------------------------------------------------------------------------------- /ward_project/residents/templates/edit_resident.html: -------------------------------------------------------------------------------- 1 | 2 | {% extends 'base.html' %} 3 | 4 | {% block title %}Dashboard - My Website{% endblock %} 5 | 6 | {% block content %} 7 |

Edit Resident

8 |
9 | {% csrf_token %} 10 | 11 | 12 | 13 |

14 | 15 | 16 | 17 |

18 | 19 | 20 | 21 |

22 | 23 | 24 | 25 |

26 | 27 | 28 | 29 |

30 | 31 | 32 | 33 |

34 | 35 | 36 |
37 | {% endblock %} -------------------------------------------------------------------------------- /ward_project/residents/templates/edit_resident_error.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block title %}Dashboard - My Website{% endblock %} 4 | 5 | {% block content %} 6 |

Error: Invalid Age Change

7 |

{{ error_message }}

8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | {% endblock %} 20 | -------------------------------------------------------------------------------- /ward_project/residents/templates/homee.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Villa Agency - Real Estate HTML5 Template 10 | 11 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 301 | 302 | 303 | -------------------------------------------------------------------------------- /ward_project/residents/templates/image/engineer-svgrepo-com.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 14 | 15 | 17 | 19 | 20 | 21 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 35 | 36 | 37 | 38 | 39 | 42 | 44 | 46 | 48 | 50 | 52 | 55 | 57 | 59 | 104 | 106 | 110 | -------------------------------------------------------------------------------- /ward_project/residents/templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | login 7 | 191 | 192 | 193 | 194 | 212 | 213 | 214 |
215 | {% block content %} 216 | {% endblock %} 217 |
218 | 219 | 226 |
227 | {% csrf_token %} 228 | 229 | 230 | 231 |
232 | 233 | 234 | -------------------------------------------------------------------------------- /ward_project/residents/templates/residents/detail.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Resident Details 5 | 41 | 42 | 43 |

{{ resident.name }}

44 |

Age: {{ resident.age }}

45 |

House No: {{ resident.house_no }}

46 |

Occupation: {{ resident.occupation }}

47 |

Business: {{ resident.business }}

48 |

No of Children: {{ resident.no_of_children }}

49 | Back to Search 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /ward_project/residents/templates/residents/search.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Residents Search 5 | 65 | 66 | 67 |

Search Residents

68 |
69 | 70 | 71 |
72 | 73 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /ward_project/residents/templates/signup.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | signup 7 | 207 | 208 | 209 | 210 | 228 | 229 | 230 |
231 | {% block content %} 232 | {% endblock %} 233 |
234 | 235 | 242 |
243 | {% csrf_token %} 244 | 245 | 246 | 250 | 251 |
252 | 253 | -------------------------------------------------------------------------------- /ward_project/residents/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /ward_project/residents/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render, get_object_or_404, redirect 2 | from .models import Resident 3 | from django.db.models import Q 4 | from django.http import HttpResponse 5 | from django.contrib.auth import authenticate, login, logout 6 | from django.contrib.auth.models import User 7 | from django.contrib.auth.decorators import login_required 8 | 9 | # To display a default error message for 404 errors in Django 10 | def custom_404_view(request, exception): 11 | return render(request, "404.html", status=404) 12 | 13 | # User Signup 14 | def signup(request): 15 | if request.method == "POST": 16 | username = request.POST["username"] 17 | password = request.POST["password"] 18 | role = request.POST["role"] # 'admin' or 'member' 19 | 20 | user = User.objects.create_user(username=username, password=password) 21 | user.save() 22 | 23 | if role == "admin": 24 | user.is_staff = True # Only one admin (set manually) 25 | else: 26 | user.is_staff = False # Regular member 27 | 28 | user.save() 29 | return redirect("login") 30 | 31 | return render(request, "signup.html") 32 | 33 | # User Login 34 | def user_login(request): 35 | if request.method == "POST": 36 | username = request.POST["username"] 37 | password = request.POST["password"] 38 | user = authenticate(request, username=username, password=password) 39 | if user: 40 | login(request, user) 41 | return redirect("dashboard") 42 | else: 43 | return HttpResponse("Invalid username or password") 44 | 45 | return render(request, "login.html") 46 | 47 | # User Logout 48 | def user_logout(request): 49 | logout(request) 50 | return redirect("login") 51 | 52 | # Dashboard with search functionality 53 | @login_required 54 | def dashboard(request): 55 | query = request.GET.get("q", "").strip() # Get search query and remove extra spaces 56 | 57 | if request.user.is_staff: # Admin sees all residents 58 | residents = Resident.objects.all() 59 | else: # Regular users see only their data 60 | residents = Resident.objects.filter(user=request.user) 61 | 62 | if query: # If search query exists, filter residents 63 | residents = residents.filter( 64 | Q(name__icontains=query) | 65 | Q(occupation__icontains=query) | 66 | Q(business__icontains=query) | 67 | Q(house_no__icontains=query) 68 | ) 69 | 70 | return render(request, "dashboard.html", {"residents": residents, "query": query}) 71 | 72 | # Add a new resident (only for members) 73 | @login_required 74 | def add_resident(request): 75 | if request.method == "POST": 76 | name = request.POST.get("name") 77 | age = request.POST.get("age") 78 | house_no = request.POST.get("house_no") 79 | occupation = request.POST.get("occupation") 80 | business = request.POST.get("business", "") 81 | no_of_children = request.POST.get("no_of_children") 82 | 83 | Resident.objects.create( 84 | user=request.user, # Assign resident to logged-in user 85 | name=name, 86 | age=age, 87 | house_no=house_no, 88 | occupation=occupation, 89 | business=business, 90 | no_of_children=no_of_children 91 | ) 92 | 93 | return redirect("dashboard") 94 | 95 | return render(request, "add_resident.html") 96 | 97 | # Edit Resident (Only Owner or Admin) 98 | @login_required 99 | def edit_resident(request, resident_id): 100 | resident = get_object_or_404(Resident, id=resident_id) 101 | 102 | if request.user != resident.user and not request.user.is_staff: 103 | return HttpResponse("You are not authorized to edit this resident.") 104 | 105 | if request.method == "POST": 106 | current_age = resident.age 107 | new_age = int(request.POST.get("age")) 108 | 109 | if not request.user.is_staff and new_age < current_age: 110 | error_message = "You cannot reduce your age. Only the admin can change it." 111 | return render(request, "edit_resident_error.html", {"error_message": error_message, "resident": resident}) 112 | 113 | resident.name = request.POST.get("name") 114 | resident.age = new_age 115 | resident.house_no = request.POST.get("house_no") 116 | resident.occupation = request.POST.get("occupation") 117 | resident.business = request.POST.get("business", "") 118 | resident.no_of_children = request.POST.get("no_of_children") 119 | 120 | resident.save() 121 | return redirect("dashboard") 122 | 123 | return render(request, "edit_resident.html", {"resident": resident}) 124 | 125 | # Delete Resident (Only Admin) 126 | @login_required 127 | def delete_resident(request, resident_id): 128 | resident = get_object_or_404(Resident, id=resident_id) 129 | 130 | if not request.user.is_staff: 131 | return HttpResponse("Only the admin can delete residents.") 132 | 133 | resident.delete() 134 | return redirect("dashboard") 135 | 136 | """ ~~~~~~~~~~~~~~~~~this is old code~~~~~~~~~~~~~~~~~ 137 | 138 | # Search functionality 139 | def search(request): 140 | query = request.GET.get("q", "") # Default to empty string if no query is provided 141 | residents = Resident.objects.all() 142 | 143 | if query: # Executes filter if query is provided 144 | residents = residents.filter( 145 | Q(name__icontains=query) | # Case-insensitive search 146 | Q(occupation__icontains=query) | 147 | Q(business__icontains=query) | 148 | Q(house_no__icontains=query) 149 | ) 150 | 151 | return render(request, "residents/search.html", {"residents": residents, "query": query}) 152 | 153 | # Display details of a single resident 154 | def resident_detail(request, resident_id): 155 | resident = get_object_or_404(Resident, id=resident_id) 156 | return render(request, "residents/detail.html", {"resident": resident}) 157 | 158 | # Delete a resident 159 | def delete_resident(request, resident_id): 160 | resident = get_object_or_404(Resident, id=resident_id) 161 | resident.delete() 162 | return redirect('search_residents') 163 | 164 | # Add a new resident 165 | def add_resident(request): 166 | if request.method == "POST": 167 | name = request.POST.get("name") 168 | occupation = request.POST.get("occupation") 169 | business = request.POST.get("business", "") 170 | house_no = request.POST.get("house_no") 171 | 172 | # Save the new resident to the database 173 | Resident.objects.create( 174 | name=name, 175 | occupation=occupation, 176 | business=business, 177 | house_no=house_no 178 | ) 179 | return HttpResponse("Resident added successfully!") 180 | 181 | # Render the form template for GET requests 182 | return render(request, "residents/add_resident.html") 183 | """ -------------------------------------------------------------------------------- /ward_project/ward_project/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KaranRajiwade/jangoBasicProject/f3391e8ec6c7b6ec95b54b402a7d2d7f4a92c8ba/ward_project/ward_project/__init__.py -------------------------------------------------------------------------------- /ward_project/ward_project/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KaranRajiwade/jangoBasicProject/f3391e8ec6c7b6ec95b54b402a7d2d7f4a92c8ba/ward_project/ward_project/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /ward_project/ward_project/__pycache__/settings.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KaranRajiwade/jangoBasicProject/f3391e8ec6c7b6ec95b54b402a7d2d7f4a92c8ba/ward_project/ward_project/__pycache__/settings.cpython-311.pyc -------------------------------------------------------------------------------- /ward_project/ward_project/__pycache__/urls.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KaranRajiwade/jangoBasicProject/f3391e8ec6c7b6ec95b54b402a7d2d7f4a92c8ba/ward_project/ward_project/__pycache__/urls.cpython-311.pyc -------------------------------------------------------------------------------- /ward_project/ward_project/__pycache__/wsgi.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KaranRajiwade/jangoBasicProject/f3391e8ec6c7b6ec95b54b402a7d2d7f4a92c8ba/ward_project/ward_project/__pycache__/wsgi.cpython-311.pyc -------------------------------------------------------------------------------- /ward_project/ward_project/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for ward_project project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ward_project.settings") 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /ward_project/ward_project/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for ward_project project. 3 | 4 | Generated by 'django-admin startproject' using Django 5.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/5.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/5.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = "django-insecure-_0qh0^8)(*)uz)&f_+gqvz0)g#n@@3wuu6*mnnb2_sb#3iffoc" 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = False 27 | ALLOWED_HOSTS = ["127.0.0.1", "localhost"] # Add your domain/IP if needed 28 | 29 | 30 | 31 | 32 | 33 | # Application definition 34 | 35 | INSTALLED_APPS = [ 36 | "django.contrib.admin", 37 | "django.contrib.auth", 38 | "django.contrib.contenttypes", 39 | "django.contrib.sessions", 40 | "django.contrib.messages", 41 | "django.contrib.staticfiles", 42 | "residents", 43 | ] 44 | 45 | MIDDLEWARE = [ 46 | "django.middleware.security.SecurityMiddleware", 47 | "django.contrib.sessions.middleware.SessionMiddleware", 48 | "django.middleware.common.CommonMiddleware", 49 | "django.middleware.csrf.CsrfViewMiddleware", 50 | "django.contrib.auth.middleware.AuthenticationMiddleware", 51 | "django.contrib.messages.middleware.MessageMiddleware", 52 | "django.middleware.clickjacking.XFrameOptionsMiddleware", 53 | ] 54 | 55 | ROOT_URLCONF = "ward_project.urls" 56 | 57 | TEMPLATES = [ 58 | { 59 | "BACKEND": "django.template.backends.django.DjangoTemplates", 60 | "DIRS": [BASE_DIR / 'residents/templates'], 61 | "APP_DIRS": True, 62 | "OPTIONS": { 63 | "context_processors": [ 64 | "django.template.context_processors.debug", 65 | "django.template.context_processors.request", 66 | "django.contrib.auth.context_processors.auth", 67 | "django.contrib.messages.context_processors.messages", 68 | ], 69 | }, 70 | }, 71 | ] 72 | 73 | WSGI_APPLICATION = "ward_project.wsgi.application" 74 | 75 | #default db is sqlite3 76 | # Database 77 | # https://docs.djangoproject.com/en/5.1/ref/settings/#databases 78 | 79 | # DATABASES = { 80 | # "default": { 81 | # "ENGINE": "django.db.backends.sqlite3", 82 | # "NAME": BASE_DIR / "db.sqlite3", 83 | # } 84 | # } 85 | 86 | #we have updated db and used mysql as a db 87 | # this is the first step selecting the database 88 | # next one is creating data-model for that selected database 89 | DATABASES = { 90 | 'default': { 91 | 'ENGINE': 'django.db.backends.mysql', 92 | 'NAME': 'ward', #database name which existed in the mysql db 93 | 'USER': 'root', # Replace with your MySQL username 94 | 'PASSWORD': 'password', # Replace with your MySQL password 95 | 'HOST': '127.0.0.1', 96 | 'PORT': '3306', 97 | } 98 | } 99 | 100 | 101 | # Password validation 102 | # https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators 103 | 104 | AUTH_PASSWORD_VALIDATORS = [ 105 | { 106 | "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", 107 | }, 108 | { 109 | "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", 110 | }, 111 | { 112 | "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", 113 | }, 114 | { 115 | "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", 116 | }, 117 | ] 118 | 119 | 120 | # Internationalization 121 | # https://docs.djangoproject.com/en/5.1/topics/i18n/ 122 | 123 | LANGUAGE_CODE = "en-us" 124 | 125 | TIME_ZONE = "UTC" 126 | 127 | USE_I18N = True 128 | 129 | USE_TZ = True 130 | 131 | 132 | # Static files (CSS, JavaScript, Images) 133 | # https://docs.djangoproject.com/en/5.1/howto/static-files/ 134 | 135 | STATIC_URL = "static/" 136 | 137 | # Default primary key field type 138 | # https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field 139 | 140 | DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" 141 | -------------------------------------------------------------------------------- /ward_project/ward_project/urls.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL configuration for ward_project project. 3 | 4 | The `urlpatterns` list routes URLs to views. For more information please see: 5 | https://docs.djangoproject.com/en/5.1/topics/http/urls/ 6 | Examples: 7 | Function views 8 | 1. Add an import: from my_app import views 9 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 10 | Class-based views 11 | 1. Add an import: from other_app.views import Home 12 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 13 | Including another URLconf 14 | 1. Import the include() function: from django.urls import include, path 15 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 16 | """ 17 | from django.contrib import admin 18 | from django.urls import path, include 19 | 20 | urlpatterns = [ 21 | path("admin/", admin.site.urls), 22 | path('', include('residents.urls')), 23 | path("login/", user_login, name="login"), 24 | path("logout/", user_logout, name="logout"), 25 | path("signup/", signup, name="signup"), 26 | ] 27 | -------------------------------------------------------------------------------- /ward_project/ward_project/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for ward_project project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ward_project.settings") 15 | 16 | application = get_wsgi_application() 17 | --------------------------------------------------------------------------------