Django完善图片管理功能

一 删除图片

1 修改mysite/image/views.py

# 删除图片功能
@login_required(login_url='/account/lobin/')
@require_POST
@csrf_exempt
def del_image(request):
    image_id = request.POST['image_id']
    try:
        image = Image.objects.get(id=image_id)
        image.delete()
        return JsonResponse({'status':"1"})
    except:
        return JsonResponse({'status':"2"})

2 配置URL

from django.conf.urls import url
from . import views

urlpatterns = [
    # 展示图像列表
    url(r'^list-images/$', views.list_images, name="list_images"),
    # 上传图片
    url(r'^upload-image/$', views.upload_image, name='upload_image'),
    # 删除图片
    url(r'^del-image/$', views.del_image, name='del_image'),
]

3 编写mysite/templates/image/list_images.html

{% extends "article/base.html" %}
{% load staticfiles %}
{% block title %}图像{% endblock %}

{% block content %}
{% for image in images %} {% empty %}

还没有图片,请点击上面的按钮添加图片

{% endfor %}
序号 标题 图片 操作
{{ forloop.counter }} {{ image.title }} {{ image.image }}
{% endblock %}

二 测试

Django完善图片管理功能_第1张图片

你可能感兴趣的:(Django)