I'll help you create a polished CRUD interface with proper styling. Let's assume you're using Django and have a model already defined. Here's a step-by-step enhancement:
- Base Template (base.html) - Include Bootstrap for styling:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CRUD Operations</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="{% url 'item-list' %}">CRUD App</a>
</div>
</nav>
<div class="container mt-4">
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
{{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
{% endif %}
{% block content %}
{% endblock %}
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
- List View Template (item_list.html):
{% extends 'base.html' %}
{% block content %}
<div class="card shadow">
<div class="card-header d-flex justify-content-between align-items-center">
<h4>Items List</h4>
<a href="{% url 'item-create' %}" class="btn btn-primary">Add New</a>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for item in object_list %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.description }}</td>
<td>
<a href="{% url 'item-update' item.id %}" class="btn btn-sm btn-warning">Edit</a>
<a href="{% url 'item-delete' item.id %}" class="btn btn-sm btn-danger">Delete</a>
</td>
</tr>
{% empty %}
<tr>
<td colspan="3" class="text-center">No items found</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}
- Create/Update Template (item_form.html):
{% extends 'base.html' %}
{% block content %}
<div class="card shadow">
<div class="card-header">
<h4>{% if form.instance.id %}Update{% else %}Create{% endif %} Item</h4>
</div>
<div class="card-body">
<form method="post">
{% csrf_token %}
<div class="mb-3">
{{ form.name.label_tag }}
{{ form.name }}
<div class="text-danger">{{ form.name.errors }}</div>
</div>
<div class="mb-3">
{{ form.description.label_tag }}
{{ form.description }}
<div class="text-danger">{{ form.description.errors }}</div>
</div>
<button type="submit" class="btn btn-success">Save</button>
<a href="{% url 'item-list' %}" class="btn btn-secondary">Cancel</a>
</form>
</div>
</div>
{% endblock %}
- Delete Template (item_confirm_delete.html):
{% extends 'base.html' %}
{% block content %}
<div class="card shadow">
<div class="card-header">
<h4>Confirm Delete</h4>
</div>
<div class="card-body">
<p>Are you sure you want to delete "{{ object }}"?</p>
<form method="post">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Delete</button>
<a href="{% url 'item-list' %}" class="btn btn-secondary">Cancel</a>
</form>
</div>
</div>
{% endblock %}
- Views Example:
from django.views.generic import ListView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.contrib import messages
from .models import Item # Replace with your model
from .forms import ItemForm # Replace with your form
class ItemListView(ListView):
model = Item
template_name = 'item_list.html'
context_object_name = 'object_list'
class ItemCreateView(CreateView):
model = Item
form_class = ItemForm
template_name = 'item_form.html'
success_url = reverse_lazy('item-list')
def form_valid(self, form):
messages.success(self.request, 'Item created successfully!')
return super().form_valid(form)
class ItemUpdateView(UpdateView):
model = Item
form_class = ItemForm
template_name = 'item_form.html'
success_url = reverse_lazy('item-list')
def form_valid(self, form):
messages.success(self.request, 'Item updated successfully!')
return super().form_valid(form)
class ItemDeleteView(DeleteView):
model = Item
template_name = 'item_confirm_delete.html'
success_url = reverse_lazy('item-list')
def form_valid(self, form):
messages.success(self.request, 'Item deleted successfully!')
return super().form_valid(form)
- Add this to your forms.py:
from django import forms
from .models import Item
class ItemForm(forms.ModelForm):
class Meta:
model = Item
fields = ['name', 'description'] # Update with your fields
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields.values():
field.widget.attrs.update({'class': 'form-control'})
Key features included:
- Responsive design with Bootstrap 5
- Card-based layout with shadows
- Hover effects on tables
- Success/error messages
- Form error handling
- Confirmation for delete operations
- Consistent styling across all pages
- Proper spacing and alignment
Make sure to:
- Replace
Item with your actual model name
- Update form fields according to your model
- Set up proper URL patterns
- Add your static files configuration
- Create appropriate messages for different operations
- Test all CRUD operations thoroughly
For even better styling, you can:
- Add custom CSS in a separate static file
- Include icons using Bootstrap Icons
- Add pagination to the list view
- Implement search functionality
- Add client-side form validation
- Use modals for delete confirmation instead of separate page