2019年11月3日 星期日

[ Django ] 影片網站資料庫及後台管理(八) - 刪除資料

1.撰寫delete.html文件

2.增加路徑urls.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""youtobeproj URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from youtobeapp.views import home, post, postform, post2, delete

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home),
    path('index/', home),
    path('post/', post),
    path('postform/', postform),
    path('post2/', post2),
    path('delete/', delete)
]

3.在views.py上撰寫delete函式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from django.shortcuts import render, redirect
from youtobeapp.models import video
from youtobeapp.form import PostForm

# Create your views here.

def home(request):
    try:
        videos = video.objects.all()
    except:
        print("沒有任何資料")
    return render(request, 'index.html', locals())
    
def post(request):
    if request.method == "POST":
        name = request.POST["videoname"]
        width = int(request.POST["videowidth"])
        height = int(request.POST["videoheight"])
        src = request.POST["videosrc"]
        v = video.objects.create(name=name, width=width, height=height, src=src)
        v.save
        return redirect('/index/')
    return render(request, 'post.html', locals())
        
def postform(request):
    postform = PostForm()
    return render(request, "postform.html", locals())
    
def post2(request):
    if request.method == "POST":
        postform = PostForm(request.POST)
        if postform.is_valid():
            name = postform.cleaned_data["name"]
            width = int(postform.cleaned_data["width"])
            height = int(postform.cleaned_data["height"])
            src = postform.cleaned_data["src"]
            v = video.objects.create(name=name, width=width, height=height, src=src)
            v.save
            return redirect('/index/')
        else:
            print("證驗有誤")
    else:
        postform = PostForm()
    return render(request, 'post2.html', locals())
    
def delete(request):
    if request.method == "POST":
        videoname=request.POST['videoname']
    try:
        v = video.objects.get(name=videoname)
        v.delete()
        return redirect('/index/')
    except:
        print("讀取錯誤")
    return render(request, "delete.html", locals())

4.啟動網站輸入刪除的影片名稱

5.刪除兩筆後的結果

上一篇文章:影片網站資料庫及後台管理(七) - 資料新增並驗證
下一篇文章:影片網站資料庫及後台管理(九) - 刪除資料輸入標籤改由選擇標籤

沒有留言:

張貼留言