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

Update view - function based view Django

編輯:Python
An update view is a view ( Logic ), Used to update specific instances of tables from the database , And provide some additional details . It is used to update entries in the database , for example , to update  geeksforgeeks  Articles on . So the update view must show the old data in the form , And let users update data only from there .Django  Provides exceptional support for updating views , But let's examine how it is done manually through a function - based view . This article focuses on updating views , Which involves Django Forms、Django Models And so on .  For update view , We need a project with some models and multiple instances , They will be displayed . Basically , Update view is a combination of detail view and create view . 

Django  Update the view —— Function based view

How to use examples to create and use update 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 we will create a for this model  Django ModelForm, stay  geeks  Create files in the folder  forms.py,
from django import forms
from .models import GeeksModel


#  create form
class GeeksForm(forms.ModelForm):

#  establish  meta  class
class Meta:
#  Specify the model to use
model = GeeksModel

#  Specify the fields to use
fields = [
"title",
"description"]

about  Update_view, You need some identification to get a specific instance of the model . Usually it is the only primary key , for example id. To specify this ID , We need to be in  urls.py  It's defined in . go to geeks/urls.py,
from django.urls import path

#  Import from view  views..py
from .views import update_view, detail_view

urlpatterns = [
path('<id>/', detail_view ),
path('<id>/update', update_view ),
]

Let's create these views with explanations . stay  geeks/views.py  in ,
from django.shortcuts import (get_object_or_404,
render,
HttpResponseRedirect)

#  Relative import of tables
from .models import GeeksModel
from .forms import GeeksForm

#  After the update, you will be redirected to  detail_View
def detail_view(request, id):
#  Initial data dictionary with field name as key
context ={}

#  Add dictionary during initialization
context[&quot;data&quot;] = GeeksModel.objects.get(id = id)

return render(request, &quot;detail_view.html&quot;, context)

#  Update view for details
def update_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)

#  Pass the object as an instance to the form
form = GeeksForm(request.POST or None, instance = obj)

#  Save the data in the form and redirect to  detail_view
if form.is_valid():
form.save()
return HttpResponseRedirect(&quot;/&quot;+id)

#  Add the form dictionary to the context
context[&quot;form&quot;] = form

return render(request, &quot;update_view.html&quot;, context)

Now create the following templates in the Templates folder ,&nbsp; stay  geeks/templates/update_view.html  in ,
<div class=&quot;main&quot;>
<!-- Create a Form -->
<form method=&quot;POST&quot;>
<!-- Security token by Django -->
{% csrf_token %}

<!-- form as paragraph -->
{{ form.as_p }}

<input type=&quot;submit&quot; value=&quot;Update&quot;>
</form>

</div>

stay  geeks/templates/detail_view.html  in ,&nbsp;
<div class=&quot;main&quot;>
<!-- Display attributes of instance -->
{{ data.title }} <br/>
{{ data.description }}
</div>

Let's check if everything is OK , visit http://localhost:8000/1/update.


Here you can see the form that has been populated with data from the instance , Now you can edit this data and easily update it , Let's check &nbsp;



Click Update and finish .&nbsp;



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