Python 从0开始 一步步基于Django创建项目(9)删除条目

本文实现的功能:

在城市列表页面,每个城市名称后,添加删除链接。

点击删除后,跳转至确认删除页面,执行删除或取消。

城市删除成功后,不在出现在城市列表页面,同时通过级联删除的方式,删除了与该城市关联的所有条目信息。

1、修改C:\D\Python\Python310\study\snap_gram\city_infos\templates\city_infos路径下的cities.html文件,添加删除链接。

{% for city in cities %}
    <li>
        <a href="{% url 'city_infos:city' city.id %}">{{ city }}</a>----
        <a href="{% url 'city_infos:delete_city' city.id %}">删除</a>
    </li>

2、修改C:\D\Python\Python310\study\snap_gram\city_infos路径下的urls.py文件,添加上一步中使用的url

#删除城市
path('delete_city/<int:city_id>/',views.delete_city,name='delete_city'),

3、修改C:\D\Python\Python310\study\snap_gram\city_infos路径下的views.py文件,添加对应上述url的视图函数

#删除城市
def delete_city(request,city_id):
    city = City.objects.get(id=city_id)

    if request.method == 'POST':
        city.delete()  # 删除城市条目
        return redirect('city_infos:cities')  # 删除成功后重定向到城市列表页面
    # 如果请求方法不是 POST,渲染一个确认删除的页面供用户确认
    return render(request, 'city_infos/delete_city.html', {'city': city})

4、新建C:\D\Python\Python310\study\snap_gram\city_infos\templates\city_infos路径下的delete_city.html

{% extends "city_infos/base.html" %}

{% block content %}
    <h1>确认:</h1>
    <p>确认删除城市:{{ city.name }}?</p>
    <form method='post'>
        {% csrf_token %}
        <button name="submit">删除</button>
    </form>
    <a href="{% url 'city_infos:cities' %}">取消</a>

{% endblock content %}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建Django项目 在命令行中运行以下命令: ``` django-admin startproject project_name ``` 2. 创建应用程序 在命令行中进入项目文件夹并运行以下命令: ``` python manage.py startapp app_name ``` 3. 配置数据库 打开项目文件夹中的settings.py文件,在DATABASES中配置数据库连接信息。 ``` DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'database_name', 'USER': 'database_user', 'PASSWORD': 'database_password', 'HOST': 'localhost', 'PORT': '3306', } } ``` 4. 创建模型 在应用程序文件夹中的models.py文件中定义模型类,如: ``` from django.db import models class User(models.Model): name = models.CharField(max_length=50) email = models.EmailField(max_length=254) password = models.CharField(max_length=50) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = 'user' ``` 5. 创建视图 在应用程序文件夹中的views.py文件中定义视图函数,如: ``` from django.shortcuts import render from .models import User def user_list(request): users = User.objects.all() context = {'users': users} return render(request, 'user_list.html', context) ``` 6. 创建模板 在应用程序文件夹中创建templates文件夹,然后在其中创建HTML模板文件,如: ``` <!DOCTYPE html> <html> <head> <title>User List</title> </head> <body> <h1>User List</h1> <ul> {% for user in users %} <li>{{ user.name }} - {{ user.email }}</li> {% endfor %} </ul> </body> </html> ``` 7. 配置URL 在应用程序文件夹中的urls.py文件中定义URL路由,如: ``` from django.urls import path from .views import user_list urlpatterns = [ path('users/', user_list, name='user_list'), ] ``` 8. 运行Django项目 在命令行中运行以下命令: ``` python manage.py runserver ``` 现在您可以在浏览器中访问http://localhost:8000/users/,查看用户列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值