0% found this document useful (0 votes)
7 views4 pages

Django Beginner Notes

Django is a high-level Python web framework designed for rapid development and clean design, featuring security measures and scalability. The document covers installation, project structure, app creation, URL routing, views, templates, models, and basic CRUD operations. It emphasizes the ease of data management through the admin panel and the use of Django's ORM for database interactions.

Uploaded by

gopalofficial00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Django Beginner Notes

Django is a high-level Python web framework designed for rapid development and clean design, featuring security measures and scalability. The document covers installation, project structure, app creation, URL routing, views, templates, models, and basic CRUD operations. It emphasizes the ease of data management through the admin panel and the use of Django's ORM for database interactions.

Uploaded by

gopalofficial00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

# Django Beginner Notes

## 1. Introduction to Django
Django is a high-level Python web framework that encourages rapid development and
clean, pragmatic design. It helps developers build secure and maintainable websites
quickly.

**Features:**
- Fast Development
- Secure (prevents SQL injection, XSS, CSRF)
- Scalable
- Fully loaded (ORM, Admin, Authentication, etc.)
- Open Source

## 2. Installation and Setup


**Step 1:** Install Django
```bash
pip install django
```
**Step 2:** Check version
```bash
django-admin --version
```
**Step 3:** Create a new project
```bash
django-admin startproject myproject
```
**Step 4:** Run the server
```bash
cd myproject
python manage.py runserver
```
Visit `http://127.0.0.1:8000/` to see your Django project running.

## 3. Project Structure
```
myproject/

├── manage.py
├── myproject/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── asgi.py
│ └── wsgi.py
```
- **manage.py**: Command-line utility for Django operations.
- **settings.py**: Project configuration.
- **urls.py**: URL routing for the project.
- **wsgi.py/asgi.py**: Server gateway interface files.

## 4. Creating an App
```bash
python manage.py startapp myapp
```
**Register the app** in `settings.py` under `INSTALLED_APPS`:
```python
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
...
'myapp',
]
```

## 5. URLs and Views


**URL routing (urls.py):**
```python
from django.urls import path
from . import views

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

**View (views.py):**
```python
from django.http import HttpResponse

def home(request):
return HttpResponse("Hello, Django!")
```

## 6. Templates
Templates are HTML files used to render dynamic content.

**Example:**
`myapp/templates/home.html`
```html
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to Django!</h1>
</body>
</html>
```

**Render template in view:**


```python
from django.shortcuts import render

def home(request):
return render(request, 'home.html')
```

## 7. Models and ORM


Django ORM allows you to interact with the database using Python code.

**Example Model:**
```python
from django.db import models

class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
grade = models.CharField(max_length=10)
def __str__(self):
return self.name
```
**Apply migrations:**
```bash
python manage.py makemigrations
python manage.py migrate
```

## 8. Admin Panel
Create superuser:
```bash
python manage.py createsuperuser
```
Login at `/admin/` to manage your models.

**Register model:**
```python
from django.contrib import admin
from .models import Student

admin.site.register(Student)
```

## 9. Forms (Basic)
```python
from django import forms

class StudentForm(forms.Form):
name = forms.CharField(max_length=100)
age = forms.IntegerField()
```

## 10. Static and Media Files


In `settings.py`:
```python
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
```

## 11. Simple CRUD Example


You can perform Create, Read, Update, and Delete operations using Django ORM
methods like:
```python
Student.objects.create(name='John', age=20, grade='A')
Student.objects.all()
Student.objects.filter(grade='A')
Student.objects.get(id=1)
Student.objects.update(age=22)
Student.objects.delete()
```

## 12. Summary
- Django is fast, secure, and scalable.
- Everything revolves around apps and models.
- The admin panel makes data management easy.
- ORM eliminates the need for raw SQL.
- Perfect for both small and large projects.

You might also like