...

Open source softwares - Django

Back to Course

Lesson Description


Lession - #1005 Django-Update Record


Updating Records

To update a record, we want the ID of the record, and we want a template with a point of interaction that let us change the values.

First we want to roll out certain improvements in the index.html format.

Modify Template

<h1>Members</h1>

<table border="1">
{% for x in mymembers %}
<tr>
<td><a href="update/{{ x.id }}">{{ x.id }}</a></td>
<td>{{ x.firstname }}</td>
<td>{{ x.lastname }}</td>
<td><a href="delete/{{ x.id }}">delete</a>
</tr>
{% endfor %}
</table>

<p>
<a href="add/">Add member</a>
</p>


View

Next, 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 index(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(request>
: first = request.POST['first'] last = request.POST['last'] member = Members(firstname=first, lastname=last>
member.save(>
return HttpResponseRedirect(reverse('index'>
>
def delete(request, id>
: member = Members.objects.get(id=id>
member.delete(>
return HttpResponseRedirect(reverse('index'>
>
def update(request, id>
: mymember = Members.objects.get(id=id>
template = loader.get_template('update.html'>
context = { 'mymember': mymember, } return HttpResponse(template.render(context, request>
>


New Template

Add another layout in the templates folder, named update.html:
members/templates/update.html:
<h1>Update member</h1>

<form action="updaterecord/{{ mymember.id }}" method="post">
{% csrf_token %}
First Name:<br>
<input name="first" value="{{ mymember.firstname }}">
<br><br>
Last Name:<br>
<input name="last" value="{{ mymember.lastname }}">
<br<<br>
<input type="submit" value="Submit">
</form>


URLs

Add a path(>
function in the members/urls.py document, that focuses the url 127.0.0.1:8000/members/update/to the right location, with the ID as 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/addrecord/', views.addrecord, name='addrecord'>
, path('delete/', views.delete, name='delete'>
, path('update/', views.update, name='update'>
, ]


What Happens on Submit?

Did you see the activity quality in the HTML form? The activity characteristic determines where to send the structure information, for this situation the structure information will be sent to:

updaterecord/{{ mymember.id }}, so we should add a path(>
function in the members/urls.py document that points to the right view:

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/addrecord/', views.addrecord, name='addrecord'>
, path('delete/', views.delete, name='delete'>
, path('update/', views.update, name='update'>
, path('update/updaterecord/', views.updaterecord, name='updaterecord'>
, ]


Code for Updating Records

Up until this point we have made the UI, and we guide the URL toward the view called updaterecord, however we have not made the view yet.

Ensure you add the updaterecord view in the in the members/views.py document:

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.render(context, request>
>
def add(request>
: template = loader.get_template('add.html'>
return HttpResponse(template.render({}, request>
>
def addrecord(request>
: x = request.POST['first'] y = request.POST['last'] member = Members(firstname=x, lastname=y>
member.save(>
return HttpResponseRedirect(reverse('index'>
>
def delete(request, id>
: member = Members.objects.get(id=id>
member.delete(>
return HttpResponseRedirect(reverse('index'>
>
def update(request, 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.POST['first'] last = request.POST['last'] member = Members.objects.get(id=id>
member.firstname = first member.lastname = last member.save(>
return HttpResponseRedirect(reverse('index'>
>