to writecode example
5 Generic Views - COMPLETE WORKING CODE EXAMPLE
[Link] (Student Model):
from [Link] import models
class Student([Link]):
name = [Link](max_length=100)
usn = [Link](max_length=15, unique=True)
email = [Link]()
marks = [Link]()
branch = [Link](max_length=10)
def __str__(self):
return f"{[Link]} ({[Link]})"
[Link] (All 5 Views):
from [Link] import ListView, DetailView
from [Link] import CreateView, UpdateView, DeleteView
from [Link] import reverse_lazy
from .models import Student
# 1. LIST VIEW 📋
class StudentListView(ListView):
model = Student
template_name = 'students/[Link]'
context_object_name = 'students'
paginate_by = 5
# 2. DETAIL VIEW 👤
class StudentDetailView(DetailView):
model = Student
template_name = 'students/[Link]'
# 3. CREATE VIEW ➕
class StudentCreateView(CreateView):
model = Student
fields = ['name', 'usn', 'email', 'marks', 'branch']
template_name = 'students/[Link]'
success_url = reverse_lazy('student_list')
# 4. UPDATE VIEW ✏️
class StudentUpdateView(UpdateView):
model = Student
fields = ['name', 'email', 'marks', 'branch']
template_name = 'students/[Link]'
success_url = reverse_lazy('student_list')
# 5. DELETE VIEW 🗑️
class StudentDeleteView(DeleteView):
model = Student
template_name = 'students/[Link]'
success_url = reverse_lazy('student_list')
[Link] (Complete Routing):
from [Link] import path
from . import views
urlpatterns = [
# 📋 LIST
path('students/', [Link].as_view(), name='student_list'),
# 👤 DETAIL
path('students/<int:pk>/', [Link].as_view(), name='student_detail'),
# ➕ CREATE
path('students/add/', [Link].as_view(), name='student_create'),
# ✏️ UPDATE
path('students/<int:pk>/edit/', [Link].as_view(), name='student_upda
# 🗑️ DELETE
path('students/<int:pk>/delete/', [Link].as_view(), name='student_de
]
Templates (Minimal):
[Link]:
<h1>Students ({{ students|length }})</h1>
<ul>
{% for student in students %}
<li>
<a href="{% url 'student_detail' [Link] %}">{{ [Link] }}</a>
- {{ [Link] }} ({{ [Link] }})
<a href="{% url 'student_update' [Link] %}">[Edit]</a>
<a href="{% url 'student_delete' [Link] %}">[Delete]</a>
</li>
{% empty %}
<li>No students</li>
{% endfor %}
</ul>
<a href="{% url 'student_create' %}">Add Student</a>
[Link]:
<h1>{{ [Link] }}</h1>
<p>USN: {{ [Link] }} | Marks: {{ [Link] }}</p>
<a href="{% url 'student_list' %}">← Back</a>
[Link] (Create/Update):
<h1>{% if [Link] %}Edit{% else %}Add{% endif %} Student</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
[Link]:
<h1>Delete {{ [Link] }}?</h1>
<form method="post">
{% csrf_token %}
<p>USN: {{ [Link] }}</p>
<button type="submit" class="btn-danger">Yes, Delete</button>
<a href="{% url 'student_list' %}">Cancel</a>
</form>
URLs & Flow:
[Link] → 📋 List all students
[Link] → 👤 Student #1 details
[Link] → ➕ Add new student
[Link] → ✏️ Edit student #1
[Link] → 🗑️ Delete student #1
Memory Hook: "LADDU CUD" ✅ FULL WORKING CODE READY FOR EXAM! 🚀
⁂
1. Full-Stack-Development_QuestionBank.docx
2. [Link]
3. [Link]
4. [Link]
5. [Link]
6. [Link]
7. [Link]
8. [Link]
9. [Link]
10. [Link]
11. Notes_Fullstack_Development_21CS62.pdf
12. Full-Stack-Development_QuestionBank.docx
13. [Link]
14. [Link]
15. [Link]
16. [Link]
17. [Link]