# Django MMAMC

## 1\. Installation & Virtual Environment Setup

### 🖥 Mac / Linux

```python
mkdir mysite
cd mysite
python3 -m venv venv
source venv/bin/activate
```

### 🖥 Windows

```python
mkdir mysite
cd mysite
python -m venv venv
venv\Scripts\activate
```

### 📦 Install Django

```python
pip install django
```

---

## 🚀 2. Create Django Project (No `.`)

```python
django-admin startproject config
cd config
```

This creates:

```python
config/
├── config/
├── manage.py
```

---

## 🌐 3. Understanding URLs (Project Level)

Open `config/urls.py`:

```python
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]
```

* This is where project-wide URLs live
    
* `admin/` is for Django’s default admin interface
    

---

## 🔐 4. Admin Panel Access

Run the server:

```python
python manage.py runserver
```

Go to http://127.0.0.1:8000/admin

Create a superuser:

```python
python manage.py createsuperuser
```

---

## 🔁 5. What are Migrations?

Django uses **migrations** to keep your database schema in sync with your models.

* `makemigrations`: Prepares changes to apply to the database
    
* `migrate`: Actually applies them
    

Run:

```python
python manage.py makemigrations
python manage.py migrate
```

---

## ⚙️ 6. Create an App

```python
python manage.py startapp core
```

Register the app in `config/settings.py`:

```python
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]
```

---

## 👋 7. Create a Simple View

Open `core/views.py`:

```python
from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to Django Students Guide 👋")
```

---

## 🔗 8. URLs in App and Project

### Create `core/urls.py`

```python
from django.urls import path
from .views import home

urlpatterns = [
    path('', home, name='home'),
]
```

### Update `config/urls.py` to include app URLs

```python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]
```

---

## 🧱 9. Models

In `core/models.py`:

```python
from django.db import models

class Student(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()

    def __str__(self):
        return self.name
```

Run:

```python
python manage.py makemigrations
python manage.py migrate
```

---

## 🧑‍💼 10. Register Model in Admin

In `core/admin.py`:

```python
from django.contrib import admin
from .models import Student

admin.site.register(Student)
```

Go to http://127.0.0.1:8000/admin and add some students.

---

## 📄 11. Use the Model in a View

In `core/views.py`:

```python
from django.shortcuts import render
from .models import Student

def home(request):
    students = Student.objects.all()
    return render(request, 'home.html', {'students': students})
```

---

## 🗂 12. Templates (Using Shared Folder & `BASE_DIR`)

### 🗃 Create a `templates/` folder beside `manage.py` (global)

Folder structure:

```python
config/
├── core/
├── templates/
│   └── home.html
├── config/
├── manage.py
```

### Update `TEMPLATES` in `config/settings.py`:

```python


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],  # GLOBAL TEMPLATES FOLDER
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                ...
            ],
        },
    },
]
```

---

## 🎨 13. Template With Tailwind CSS

Create `templates/home.html`:

```python
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Student List</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 text-gray-800 font-sans">

    <div class="max-w-4xl mx-auto p-8">
        <h1 class="text-3xl font-bold mb-6 text-center text-blue-700">Student List</h1>

        <div class="bg-white shadow-md rounded-lg p-6">
            <table class="w-full table-auto">
                <thead>
                    <tr class="bg-blue-100">
                        <th class="p-2 text-left border">Name</th>
                        <th class="p-2 text-left border">Age</th>
                    </tr>
                </thead>
                <tbody>
                    {% for student in students %}
                        <tr class="hover:bg-gray-50">
                            <td class="p-2 border">{{ student.name }}</td>
                            <td class="p-2 border">{{ student.age }}</td>
                        </tr>
                    {% empty %}
                        <tr>
                            <td colspan="2" class="p-4 text-center text-gray-500">No students found.</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

</body>
</html>
```

---

## ✅ Final Steps

```python
python manage.py runserver
```

Visit: http://127.0.0.1:8000

You’ll see a beautiful **Tailwind-styled student list** coming from your database 🎉

---

## 🧠 Recap Table

| Step | Description |
| --- | --- |
| Virtualenv | Isolated environment |
| Install Django | Framework setup |
| Startproject / App | Structure your code |
| URL routing | Connect views to URLs |
| Admin Panel | Manage data easily |
| Migrations | Manage DB schema |
| Models | Define data |
| Views | Control logic |
| Templates | Display content |
| Tailwind CSS | Beautiful design |
| BASE\_DIR | Standardize template paths |

---
