程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Delete view - function based view Django

編輯:Python
  • Personal website :【 Hai Yong 】【 Fishing games 】【 Developing documents 】
  • 🤟 Interesting and humorous artificial intelligence learning website : Artificial intelligence
  • Want to find a little partner to learn and communicate together , Please click on 【 Full stack technology exchange group 】
  • Free and practical Java Brush problem ( Daquan of facial scriptures ) Website : Click to jump to the website

Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 29 God , Click to see the event details

Deleting a view is a view that deletes a specific instance of a table from the database ( Logic ). It is used to delete entries in the database , for example , Delete geeksforgeeks Articles on . So deleting a view must display a confirmation message to the user , And the instance should be deleted automatically .Django Provides exceptional support for deleting views , But let's examine how it is done manually through a function - based view . This article focuses on deleting views , Which involves Django Forms、Django Models And so on . 

For delete view , We need a project that contains some models and multiple instances that can be deleted .

Django Delete view —— Function based view

How to use examples to create and use delete views Explanation . Consider a person named geeksforgeeks Project , It has a name called geeks Applications for . 

After you have a project and an application , Let's create a model , We will create an instance of it from our view . stay geeks/models.py in ,  

# Import standards from built-in Libraries Django Model
from django.db import models
# Declare a “GeeksModel” The new model of
class GeeksModel(models.Model):
# The model field
title = models.CharField(max_length = 200)
description = models.TextField()
# Rename the instance of the model with the title name
def __str__(self):
return self.title

After creating this model , We need to run two commands to create a database for it . 

Python manage.py makemigrations
Python manage.py migrate

Now let's use shell Create some instances of this model , Run the form bash, 

Python manage.py shell

Enter the following command  

>>> from geeks.models import GeeksModel
>>> GeeksModel.objects.create(
title="title1",
description="description1").save()
>>> GeeksModel.objects.create(
title="title2",
description="description2").save()
>>> GeeksModel.objects.create(
title="title2",
description="description2").save()

Now we are ready for the back end . Verify that the has been removed from **http://localhost:8000/admin/geeks/geeksmodel/** Create examples

Now let's first create our delete view , stay geeks/views.py 

from django.shortcuts import (get_object_or_404,
render,
HttpResponseRedirect)
from .models import GeeksModel
# Delete view for details
def delete_view(request, id):
# Initial data dictionary with field name as key
context ={}
# Acquired and delivered id Related objects
obj = get_object_or_404(GeeksModel, id = id)
if request.method =="POST":
# Delete object
obj.delete()
# After deleting redirection to the home page
return HttpResponseRedirect("/")
return render(request, "delete_view.html", context)

Now use id The regular expression of is mapped to the... Of the view url,  stay geeks/urls.py 

from django.urls import path
# importing views from views..py
from .views import delete_view
urlpatterns = [
path('<id>/delete', delete_view ),
]

Deleting a view template includes a simple form , Used to confirm whether the user wants to delete the instance . stay geeks/templates/delete_view.html in ,

<div class="main">
<!-- Create a Form -->
<form method="POST">
<!-- Security token by Django -->
{% csrf_token %}
Are you want to delete this item ?
<input type="submit" value="Yes" />
<a href="/">Cancel </a>
</form>
</div>

Everything is all set. , Now let's check if it works , visit **http://localhost:8000/2/delete** 

Let's check if the instance has been deleted , 

It can be used as needed obj.delete() Function implements this view in any way .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved