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

Django Concepts Explained

The document outlines key concepts of Django, including its MVT architecture, the purpose of virtual environments, CRUD operations, API and RESTful API definitions, middleware functionality, the significance of SQL in Django, and components of Django ORM. It provides examples such as a blog app and a todo app to illustrate these concepts. Each section succinctly explains the role and usage of various Django features and practices.

Uploaded by

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

Django Concepts Explained

The document outlines key concepts of Django, including its MVT architecture, the purpose of virtual environments, CRUD operations, API and RESTful API definitions, middleware functionality, the significance of SQL in Django, and components of Django ORM. It provides examples such as a blog app and a todo app to illustrate these concepts. Each section succinctly explains the role and usage of various Django features and practices.

Uploaded by

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

1.

Django MVT (Model-View-Template) Architecture with Example

------------------------------------------------------------

Django uses the MVT architecture:

- Model: Handles the data and database.

- View: Contains business logic.

- Template: Deals with presentation.

Example: Blog App

- Model:

class Post(models.Model):

title = models.CharField(max_length=100)

content = models.TextField()

- View:

def post_list(request):

posts = Post.objects.all()

return render(request, 'blog/post_list.html', {'posts': posts})

- Template:

{% for post in posts %}

<h2>{{ post.title }}</h2>

<p>{{ post.content }}</p>

{% endfor %}

2. What is a Virtual Environment (virtualenv) and Why It's Used

---------------------------------------------------------------
- Isolated space for Python projects.

- Keeps dependencies separate.

- Avoids conflicts between packages.

Commands:

python -m venv myenv

source myenv/bin/activate (Windows: myenv\Scripts\activate)

3. CRUD Operations in Django with Example

----------------------------------------

CRUD = Create, Read, Update, Delete

Example: Todo App

- Model:

class Task(models.Model):

title = models.CharField(max_length=100)

- Create:

Task.objects.create(title="New Task")

- Read:

Task.objects.all()

- Update:

task = Task.objects.get(id=1)

task.title = "Updated Task"

task.save()
- Delete:

task = Task.objects.get(id=1)

task.delete()

4. API and RESTful API in Brief

-------------------------------

- API: Allows different software to communicate.

- RESTful API: Follows REST rules, uses HTTP methods.

Example:

GET /posts -> Get posts

POST /posts -> Create post

5. Django Middleware and Its Role

---------------------------------

- Middleware processes requests/responses globally.

- Can modify request before view or response after view.

Example:

class SimpleMiddleware:

def __init__(self, get_response):

self.get_response = get_response

def __call__(self, request):

print("Request received")

response = self.get_response(request)

print("Response returned")
return response

6. Significance of SQL in Django

--------------------------------

- Django converts Python ORM code to SQL queries.

- Avoids writing raw SQL.

Example:

Post.objects.all() -> SELECT * FROM blog_post;

7. Key Components of Django ORM

-------------------------------

- Model class: Defines table.

- QuerySet: Set of database queries.

- Manager: Interface for querying.

Example:

class Product(models.Model):

name = models.CharField(max_length=100)

Product.objects.create(name="Phone")

Product.objects.all()

Product.objects.filter(name="Phone")

You might also like