0

This is my code in my html

{% for summary in psummary %}
<tr>
<td colspan="3" class="tdcell">{{summary.Description}}</td>
<td colspan="2" class="tdcell">{{summary.Start_Grading_Period}}</td>
<td colspan="2" class="tdcell">{{summary.End_Grading_Period}}</td>
<td colspan="2" class="tdcell">{{summary.Method}}</td>
<td colspan="3" class="tdcell"><span style="text-align: right;font-weight: 600;" class="close" onclick="deleteRow(this)"><a href="url:delete_view">&times;</a></span></td>
</tr>
{% endfor %}

this is how it looks like

enter image description here

this is my views.py

def function(request,part_id =None):
    object = gradingPeriodsSummary.objects.get(id=part_id)
    object.delete()

    print(object)
    return render(request, 'admin/Homepage/view.html')

urls.py

path('delete/(?P<part_id>[0-9]+)/$', Homepage.views.function, name='delete_view'),

i just want that if the teacher click the close button it will delete the record in the database

i followed the instruction here, but its not working Django - How to delete a object directly from a button in a table

2
  • You will get an error on print(object) as it's .... deleted. Beside that, your code looks correct, what's the matter? Commented Nov 28, 2019 at 6:23
  • i dont receive an error, but the data didnt delete Commented Nov 28, 2019 at 6:24

2 Answers 2

0

You are not passing the id to the URL. Try adding the id to your link:

<a href="{% url "delete_view" summary.id %}" >&times;</a>
Sign up to request clarification or add additional context in comments.

3 Comments

i copy your solution sir, but the record still exist when i delete the record in the html
You can try the above, change the url definition but still pass the id
Update your href attribute to the example in the code above.
0

wrong url definition href="url:delete_view" must be href="{% url "delete_view" summary.id %}", use this

<td colspan="3" class="tdcell"><span style="text-align: right;font-weight: 600;" class="close" onclick="deleteRow(this)"><a href="{% url "delete_view" summary.id %}">&times;</a></span></td>

instead of using

<td colspan="3" class="tdcell"><span style="text-align: right;font-weight: 600;" class="close" onclick="deleteRow(this)"><a href="url:delete_view">&times;</a></span></td>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.