/curd_app

Primary LanguagePython

Django CRUD (Create, Read, Update, Delete) operation for a "Product" model.

This includes the models, views, forms, templates, and URLs to implement the CRUD functionalities.

  1. Set up your Django project and app: Follow the same steps as mentioned in the previous answer to create a new Django project and app.

  2. Set up your Django project and app: Ensure you have Django installed. If not, you can install it using pip:

pip install django

Create a new Django project and app:

django-admin startproject myproject
cd myproject
django-admin startapp myapp
  1. Define the model: Open the models.py file inside your app (myapp) and define the model for the Product:
# myapp/models.py
from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    price = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return self.name
  1. Create database tables: Run the following command to create the database tables based on your models:
python manage.py makemigrations
python manage.py migrate
  1. Create forms for the Product model: Create a forms.py file inside your app and define a form for the Product model:
# myapp/forms.py
from django import forms
from .models import Product

class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ['name', 'description', 'price']
  1. Create views for CRUD operations: Open the views.py file inside your app and define the views for CRUD operations:
# myapp/views.py
from django.shortcuts import render, get_object_or_404, redirect
from .models import Product
from .forms import ProductForm

def product_list(request):
    products = Product.objects.all()
    return render(request, 'product_list.html', {'products': products})

def product_detail(request, pk):
    product = get_object_or_404(Product, pk=pk)
    return render(request, 'product_detail.html', {'product': product})

def product_create(request):
    if request.method == 'POST':
        form = ProductForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('product_list')
    else:
        form = ProductForm()
    return render(request, 'product_form.html', {'form': form})

def product_update(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == 'POST':
        form = ProductForm(request.POST, instance=product)
        if form.is_valid():
            form.save()
            return redirect('product_list')
    else:
        form = ProductForm(instance=product)
    return render(request, 'product_form.html', {'form': form})

def product_delete(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == 'POST':
        product.delete()
        return redirect('product_list')
    return render(request, 'product_confirm_delete.html', {'product': product})
  1. Create templates: Create templates for rendering the views. Inside your app's folder, create a new folder called templates, and inside it, create another folder named myapp. Within the myapp folder, create the following templates:

product_list.html:

<!DOCTYPE html>
<html>
<head>
    <title>Product List</title>
</head>
<body>
    <h1>Product List</h1>
    <ul>
        {% for product in products %}
            <li><a href="{% url 'product_detail' product.pk %}">{{ product.name }}</a></li>
        {% endfor %}
    </ul>
    <a href="{% url 'product_create' %}">Add New Product</a>
</body>
</html>

product_detail.html:

<!DOCTYPE html>
<html>
<head>
    <title>Product Detail</title>
</head>
<body>
    <h1>{{ product.name }}</h1>
    <p>Description: {{ product.description }}</p>
    <p>Price: {{ product.price }}</p>
    <a href="{% url 'product_list' %}">Back to Product List</a>
</body>
</html>

product_form.html:

<!DOCTYPE html>
<html>
<head>
    <title>{% if form.instance.pk %}Update Product{% else %}Create Product{% endif %}</title>
</head>
<body>
    <h1>{% if form.instance.pk %}Update Product{% else %}Create Product{% endif %}</h1>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Save</button>
    </form>
    <a href="{% url 'product_list' %}">Back to Product List</a>
</body>
</html>

product_confirm_delete.html:

<!DOCTYPE html>
<html>
<head>
    <title>Delete Product</title>
</head>
<body>
    <h1>Delete Product</h1>
    <p>Are you sure you want to delete "{{ product.name }}"?</p>
    <form method="post">
        {% csrf_token %}
        <button type="submit">Confirm Delete</button>
    </form>
    <a href="{% url 'product_list' %}">Back to Product List</a>
</body>
</html>
  1. Set up URLs: In the urls.py file inside your app, define the URLs and map them to the corresponding views:
# myapp/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.product_list, name='product_list'),
    path('product/<int:pk>/', views.product_detail, name='product_detail'),
    path('product/create/', views.product_create, name='product_create'),
    path('product/update/<int:pk>/', views.product_update, name='product_update'),
    path('product/delete/<int:pk>/', views.product_delete, name='product_delete'),
]
  1. Include the app URLs in the project URLs: In the main project's urls.py file (located at the same level as your app folder), include the app URLs:
# myproject/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('myapp/', include('myapp.urls')), # Replace 'myapp' with your app's name
]
  1. Run the development server: Finally, run the development server to see your CRUD operations in action:
python manage.py runserver

Now, you can access your app in the browser at http://127.0.0.1:8000/myapp/. You should be able to view, create, update, and delete products using the provided views and templates.