In myapp/models.py
, define a simple model:
from django.db import models class Item(models.Model): name = models.CharField(max_length=100) description = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) def __str__(self): return self.name
Run the migrations to create the database schema:
python manage.py makemigrations python manage.py migrate
In myapp/views.py
, create views for handling CRUD operations:
from django.shortcuts import render, get_object_or_404, redirect from .models import Item from .forms import ItemForm def item_list(request): items = Item.objects.all() return render(request, 'myapp/item_list.html', {'items': items}) def item_detail(request, pk): item = get_object_or_404(Item, pk=pk) return render(request, 'myapp/item_detail.html', {'item': item}) def item_create(request): if request.method == "POST": form = ItemForm(request.POST) if form.is_valid(): item = form.save() return redirect('item_detail', pk=item.pk) else: form = ItemForm() return render(request, 'myapp/item_form.html', {'form': form}) def item_update(request, pk): item = get_object_or_404(Item, pk=pk) if request.method == "POST": form = ItemForm(request.POST, instance=item) if form.is_valid(): item = form.save() return redirect('item_detail', pk=item.pk) else: form = ItemForm(instance=item) return render(request, 'myapp/item_form.html', {'form': form}) def item_delete(request, pk): item = get_object_or_404(Item, pk=pk) if request.method == "POST": item.delete() return redirect('item_list') return render(request, 'myapp/item_confirm_delete.html', {'item': item})