0% found this document useful (0 votes)
20 views

Django Project Structure 3

The document describes creating a Django web application using class-based views to create, retrieve, update and delete information about Indian states. It includes models, views, templates and URLs to list, view, add, edit and delete state records.

Uploaded by

Mahesh Kumar
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)
20 views

Django Project Structure 3

The document describes creating a Django web application using class-based views to create, retrieve, update and delete information about Indian states. It includes models, views, templates and URLs to list, view, add, edit and delete state records.

Uploaded by

Mahesh Kumar
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

Write a Django application using class based views: ListView,

DetailView, CreateView, UpdateView, DeleteView.


The Django application will be used to create, retrieve, update and
delete Indian states information.
Create a model with name State with fields: name, population,
language, capital.
The app should be able to show the list of states, detail of a single
state, create a state, update a state and delete a state.
Create necessary templates, models, forms, urls.
Program:
Models.py
from django.db import models

class State(models.Model):
name = models.CharField(max_length=100)
population = models.PositiveIntegerField()
language = models.CharField(max_length=50)
capital = models.CharField(max_length=100)

def __str__(self):
return self.name

views.py
# stateapp/views.py
from django.urls import reverse_lazy
from django.views.generic import ListView, DetailView, CreateView, UpdateView,
DeleteView
from .models import State

class StateListView(ListView):
model = State
template_name = 'stateapp/state_list.html'
context_object_name = 'states'

class StateDetailView(DetailView):
model = State
template_name = 'stateapp/state_detail.html'
context_object_name = 'state'

class StateCreateView(CreateView):
model = State
template_name = 'stateapp/state_form.html'
fields = ['name', 'population', 'language', 'capital']

class StateUpdateView(UpdateView):
model = State
template_name = 'stateapp/state_form.html'
fields = ['name', 'population', 'language', 'capital']

class StateDeleteView(DeleteView):
model = State
template_name = 'stateapp/state_confirm_delete.html'
success_url = reverse_lazy('state_list')

state_list.html
<!-- stateapp/templates/stateapp/state_list.html -->
{% extends 'stateapp/base.html' %}

{% block content %}
<h1>Indian States</h1>
<ul>
{% for state in states %}
<li><a href="{% url 'state_detail' state.pk %}">{{ state.name }}</a></li>
{% endfor %}
</ul>
<a href="{% url 'state_create' %}">Add New State</a>
{% endblock %}

State_detail.html
{% extends 'stateapp/base.html' %}

{% block content %}
<h1>{{ state.name }} Details</h1>
<p>Population: {{ state.population }}</p>
<p>Language: {{ state.language }}</p>
<p>Capital: {{ state.capital }}</p>
<a href="{% url 'state_edit' state.pk %}">Edit</a>
<a href="{% url 'state_delete' state.pk %}">Delete</a>
<a href="{% url 'state_list' %}">Back to States</a>
{% endblock %}

State_form.html
{% extends 'stateapp/base.html' %}

{% block content %}
<h1>{% if state %}Edit{% else %}Create{% endif %} State</h1>
<form method="post" action="{% if state %}{% url 'state_edit' state.pk %}{%
else %}{% url 'state_create' %}{% endif %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
<a href="{% url 'state_list' %}">Cancel</a>
{% endblock %}

State_confirm_delete.html
{% extends 'stateapp/base.html' %}

{% block content %}
<h1>Confirm Deletion</h1>
<p>Are you sure you want to delete {{ state.name }}?</p>
<form method="post" action="{% url 'state_delete' state.pk %}">
{% csrf_token %}
<button type="submit">Confirm</button>
</form>
<a href="{% url 'state_list' %}">Cancel</a>
{% endblock %}

Base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}Indian States{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

Urls.py\stateapp
# stateapp/urls.py
from django.urls import path
from .views import StateListView, StateDetailView, StateCreateView,
StateUpdateView, StateDeleteView

urlpatterns = [
path('', StateListView.as_view(), name='state_list'),
path('<int:pk>/', StateDetailView.as_view(), name='state_detail'),
path('new/', StateCreateView.as_view(), name='state_create'),
path('<int:pk>/edit/', StateUpdateView.as_view(), name='state_edit'),
path('<int:pk>/delete/', StateDeleteView.as_view(), name='state_delete'),
]

Urls.py\state_project
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('states/', include('stateapp.urls')),
]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy