tried deleted data but failed to delete from django database

I have created models.py, views.py, and urls.py and later on accordingly updated data.html file but when I click on the delete button it gives me an error. so the error file also attached for reference. Help appreciated and waiting for resolution.

Error file

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/delete/
Using the URLconf defined in student.urls, Django tried these URL patterns, in this order:

admin/
[name='index']
export/ [name='export']
export-pdf [name='export-pdf']
register/ [name='register']
login/ [name='login']
home/ [name='home']
logout/ [name='logout']
upload/ [name='upload']
result/ [name='result']
dashbord/ [name='dashbord']
data/ [name='data']
delete/<int:id>
^static/(?P<path>.*)$
The current path, delete/, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

urls.py

 path('data/', views.data, name='data'),
 path('delete/<int:id>', views.delete),

data.html

<a href="/edit/{{ student.id }}"  class="btn btn-success"><span>Edit</span></a          
<a href="/delete/{{data1.id}}" class="btn btn-danger">Delete</a>

views.py

def data(request):
    data1 = Contact.objects.all()
    # myFilter = OrderFilter()
    dict = {
        "data1":data1
    }
    

    return render(request, 'data.html', context=dict)

# Delete Data
def delete(request):
    data1 = Contact.objects.all(id=id)
    data1.delete()
    return redirect("/data")

model.py

from django.db import models
from django.db.models.fields import CharField
from django.contrib import admin

# Create your models here.


class Contact(models.Model):
    name = models.CharField(max_length=50, default="")
    contact = models.CharField(max_length=50, default='')
    address = models.TextField(max_length=50, default='')
    program = models.CharField(max_length=50, default='')
    email = models.CharField(max_length=50, primary_key=True, null=False, unique=True)
    w3review = models.TextField(max_length=60, default="")

    def __str__(self):
        return self.name

class Cv(models.Model):
    filename = models.CharField(max_length=20)
    upload = models.FileField(upload_to='cv')

    def __str__(self):
        return self.filename
  • What is the url rendered in your html?

  • What is the type of data in data1 in your request?

If you don’t understand why the answer to my second question answers your question, then I would suggest you work their way through either (or both) of the Official Django Tutorial and the Django Girls Tutorial. Those tutorials will provide you the knowledge that you need to fully understand the relationships between urls, views, templates, and models.

1 Like

data1 function

    data1 = Contact.objects.all()
    # myFilter = OrderFilter()
    dict = {
        "data1":data1
    }
    

    return render(request, 'data.html', context=dict) ```

/delete is rendered automatacally. Error coming at http://127.0.0.1:8000/delete/


```# Page not found (404)

|Request Method:|GET|
| --- | --- |
|Request URL:|http://127.0.0.1:8000/delete/|

Using the URLconf defined in `student.urls` , Django tried these URL patterns, in this order:

1. admin/
2. [name='index']
3. export/ [name='export']
4. export-pdf [name='export-pdf']
5. register/ [name='register']
6. login/ [name='login']
7. home/ [name='home']
8. logout/ [name='logout']
9. upload/ [name='upload']
10. result/ [name='result']
11. dashbord/ [name='dashbord']
12. data/ [name='data']
13. delete/<int:id>
14. ^static/(?P<path>.*)$

The current path, `delete/` , didn’t match any of these.

You’re seeing this error because you have `DEBUG = True` in your Django settings file. Change that to `False` , and Django will display a standard 404 page.```