How To Update Saved Value In Template Django
Django Update Record
Updating Records
To update a tape, we need the ID of the record, and we need a template with an interface that let us change the values.
Get-go we need to brand some changes in the alphabetize.html template.
Modify Template
Start by adding a link for each member in the table:
members/templates/index.html:
<h1>Members</h1> <table border="ane"> {% for 10 in mymembers %} <tr> <td><a href="update/{{ x.id }}">{{ x.id }}</a></td> <td>{{ x.firstname }}</td> <td>{{ 10.lastname }}</td> <td><a href="delete/{{ x.id }}">delete</a> </tr> {% endfor %} </tabular array> <p> <a href="add together/">Add member</a> </p> The link goes to a view chosen update with the ID of the electric current fellow member.
The issue volition look like this:
View
Side by side, add the update view in the members/views.py file:
members/views.py:
from django.http import HttpResponse, HttpResponseRedirect from django.template import loader from django.urls import reverse from .models import Members def alphabetize(request): mymembers = Members.objects.all().values() template = loader.get_template('index.html') context = { 'mymembers': mymembers } return HttpResponse(template.render(context, request)) def add(request): template = loader.get_template('add.html') return HttpResponse(template.render({}, request)) def addrecord(asking): beginning = asking.Postal service['starting time'] last = request.Mail['last'] fellow member = Members(firstname=first, lastname=final) fellow member.relieve() return HttpResponseRedirect(reverse('alphabetize')) def delete(asking, id): fellow member = Members.objects.go(id=id) member.delete() return HttpResponseRedirect(opposite('index')) def update(request, id): mymember = Members.objects.become(id=id) template = loader.get_template('update.html') context = { 'mymember': mymember, } return HttpResponse(template.render(context, asking)) The update view does the post-obit:
- Gets the
idas an argument. - Uses the
idto locate the correct record in the Members table. - loads a template chosen
update.html. - Creates an object containing the fellow member.
- Sends the object to the template.
- Outputs the HTML that is rendered by the template.
New Template
Add a new template in the templates folder, named update.html:
members/templates/update.html:
<h1>Update member</h1> <form action="updaterecord/{{ mymember.id }}" method="post"> {% csrf_token %} Start Proper name:<br> <input name="first" value="{{ mymember.firstname }}"> <br><br> Terminal Name:<br> <input proper name="last" value="{{ mymember.lastname }}"> <br><br> <input blazon="submit" value="Submit"> </course> The template contains an HTML form with the values from the selected member.
Notation: Django requires this line in the class:
{% csrf_token %}
to handle Cantankerous Site Asking Forgeries in forms where the method is Postal service.
URLs
Add together a path() function in the members/urls.py file, that points the url 127.0.0.1:8000/members/update/ to the correct location, with the ID equally a parameter:
members/urls.py:
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), path('add/', views.add, name='add'), path('add together/addrecord/', views.addrecord, name='addrecord'), path('delete/<int:id>', views.delete, name='delete'), path('update/<int:id>', views.update, name='update'), ] In the browser, click the ID of the member you want to change and the upshot should look like this:
What Happens on Submit?
Did you find the action attribute in the HTML grade? The activeness aspect specifies where to send the form data, in this case the grade data will exist sent to:
updaterecord/{{ mymember.id }}, so we must add a path() function in the members/urls.py file that points to the right view:
members/urls.py:
from django.urls import path from . import views urlpatterns = [ path('', views.index, proper name='index'), path('add/', views.add together, proper noun='add'), path('add/addrecord/', views.addrecord, proper noun='addrecord'), path('delete/<int:id>', views.delete, name='delete'), path('update/<int:id>', views.update, name='update'), path('update/updaterecord/<int:id>', views.updaterecord, name='updaterecord'), ] Code for Updating Records
So far nosotros have fabricated the user interface, and we point the URL to the view chosen updaterecord, but nosotros take non made the view even so.
Make sure you add the updaterecord view in the in the members/views.py file:
members/views.py:
from django.http import HttpResponse, HttpResponseRedirect from django.template import loader from django.urls import reverse from .models import Members def index(request): mymembers = Members.objects.all().values() template = loader.get_template('index.html') context = { 'mymembers': mymembers, } return HttpResponse(template.return(context, request)) def add together(asking): template = loader.get_template('add.html') render HttpResponse(template.render({}, request)) def addrecord(request): 10 = request.Mail['starting time'] y = request.Post['concluding'] member = Members(firstname=x, lastname=y) member.save() return HttpResponseRedirect(reverse('alphabetize')) def delete(request, id): member = Members.objects.go(id=id) fellow member.delete() return HttpResponseRedirect(reverse('index')) def update(asking, id): mymember = Members.objects.get(id=id) template = loader.get_template('update.html') context = { 'mymember': mymember, } return HttpResponse(template.render(context, request)) def updaterecord(request, id): first = request.Mail['starting time'] last = request.POST['last'] member = Members.objects.get(id=id) member.firstname = starting time member.lastname = last member.save() return HttpResponseRedirect(reverse('index')) The updaterecord office will update the tape in the members table with the selected ID.
Try to update a record and run across how information technology works:
If you press the submit button, the members tabular array should take been updated:
How To Update Saved Value In Template Django,
Source: https://www.w3schools.com/django/django_update_record.php
Posted by: gordonlievaight.blogspot.com

0 Response to "How To Update Saved Value In Template Django"
Post a Comment