Web page -form-update - not working

10 views
Skip to first unread message

Body Abdo

unread,
Apr 18, 2020, 11:58:46 AM4/18/20
to Django users
Hi all;
I have a problem when edit my form ... my page doesn't update

models.py
class Hard_number(models.Model):
hard = (
('SD1', 'SDD1'),
('SD2', 'SDD2'),
('SD3', 'SDD3'),
('SD4', 'SDD4'),
('SD5', 'SDD5'),
('SD6', 'SDD6'),
('SD7', 'SDD7'),
('SD8', 'SDD8'),
('SD9', 'SDD9'),
('SD10','SDD10'),
('SD11','SDD11'),
('SD12','SDD12'),
('SD13','SDD13'),
('SD14','SDD14'),
('SDD15','SDD15'),

)
hard_n = models.CharField(max_length=20, choices=hard)
lend_period = models.CharField(max_length=20)
lend_by = models.CharField(max_length=20)
due_back= models.DateField(null=True, blank=False)

LOAN_STATUS = (
('d', 'Maintaince'),
('o', 'On Loan'),
('a', 'Available')
)
status = models.CharField(max_length=1, choices=LOAN_STATUS, blank= True, default='a')

def __str__(self):
return self.hard_n

class Meta:
ordering = ['due_back']
verbose_name = 'Hards'
verbose_name_plural = 'HARDS'
db_table = 'Hard_number'


def get_absolute_url(self):
return reverse('archive:show',args=[str(self.id)])




views.py
def hard_form(request):
if request.method == "POST":
form = Hard_numberForm(request.POST)
if form.is_valid():
try:
form.save()
return redirect('archive:show')
except:
pass
else:
form = Hard_numberForm()
return render(request, 'hardindex.html', {'form': form})

# https://www.javatpoint.com/django-crud-application

def show_hard(request):
hard = Hard_number.objects.all()
return render(request, "show.html", {"hard": hard})


def edit_hard(request, id ):
hards_update = Hard_number.objects.get(id=id)
return render(request, 'hard_edit.html',{'hards_update': hards_update})


def update_hard(request, id):
hards_update = Hard_number.objects.get(id=id)
form = Hard_numberForm(request.POST,instance=hards_update)
if form.is_valid():
form.save()
return redirect('archive:update')
return render(request, 'hard_edit.html', {'hards_update': hards_update})



urls.py
path('form_hard', views.hard_form, name='form_hard'),
path('form_hard/show', views.show_hard, name='show'),
#path('form_hard/show/edit/<int:id>', views.edit_hard, name='edit'),
path('form_hard/show/update/<int:id>', views.update_hard, name='update'),

hard_edit.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit</title>
{% load static %}
<link rel ="stylesheet" href="{% static 'css/lab.css'%}"/>
</head>
<body>
<form method ="post" class ="post-form" action="{% url 'archive:show'%}">
{%csrf_token %}
<div class="container">
<br>

<div class="form-group row">
<label class="col-md-12 col-form-label"></label>
<div class="col-md-4">
<h3> Update Details</h3>
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Hard number</label>
<div class="col-md-4">
<input type="text" name="hard_n" value="{{hards_update.hard_n}}">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label"> Lend period</label>
<div class="col-md-12">
<input type="text" name="lend_period" value="{{hards_update.lend_period}}">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Lend by </label>
<div class="col-md-4">
<input type="text" name="lend" value="{{hards_update.lend_by }}">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label"> Due back</label>
<div class="col-md-4">
<input type="text" name="Due" value="{{hards_update.due_back}}">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label"> status</label>
<div class="col-md-">
<input type="text" value="{{hards_update.status}}">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label"></label>
</div>
<button type ="submit" class="btn btn-outline-dark btn-lg"> Update</button>
</div>

</form>
</body>
</html>

Kasper Laudrup

unread,
Apr 18, 2020, 12:22:16 PM4/18/20
to django...@googlegroups.com


On 18/04/2020 17.58, Body Abdo wrote:
> Hi all;
> I have a problem when edit my form ... my page doesn't update
>

I would start by not ignoring all errors like you do with:

except:
pass

Errors should be handled or at least logged, not ignored.

That would most likely tell you why it's not working so you don't have
to guess.

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages