how do I update/delete a content in Django?

Viewed 966

So I have a form in Django that lets users write their diary.
Now I want to add edit button and delete button, but I'm having error. So here are the codes.
template (detail.html)

...
<form method='post' class="form-group">
    {% csrf_token %}
    <div class="row justify-content-center">
        <a href="{% url 'delete' authuser_id slug %}">
            <button class='btn btn-primary button-delete'>Delete</button>
        </a>
    </div>
</form>

urls.py

...
urlpatterns = [
...
    path('detail/<int:authuser_id>/<slug:slug>', views.detail, name='detail'),
    path('detail/<int:authuser_id>/<slug:slug>', views.delete, name='delete'),
]

views.py

from .models import DiaryInput
...

def detail(request, authuser_id, slug):
    todayDiary = DiaryInput.objects.get(slug=slug)
    return render(request, '/detail.html', {'todayDiary' : todayDiary})

def delete(request, authuser_id, slug):
    todayDiary = DiaryInput.objects.get(slug=slug)
    todayDiary.delete()
    return redirect('/')

When I go to the detail page of a specific diary, I get an error that says :

Reverse for 'delete' with arguments '('',)' not found. 1 pattern(s) tried: ['detail/detail/(?P[0-9]+)/(?P[-a-zA-Z0-9_]+)/delete$']

I believe there is something wrong with my template, in the {% url %} tag, but I don't see what I've done wrong. I appreciate your help :)

2 Answers
views.py

def add(request):
    if request.method == 'POST':
        form = EditEntryForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')             
    else:
        form = AddEntryForm()
    return render(request, '/add.html', {'form':form})

def update(request, id):
    entry = get_object_or_404(Entry, id=id)
    if request.method == 'POST':
        form = EditEntryForm(data=request.POST, instance=entry)
        if form.is_valid():
            form.save()
            return redirect('home')             
    else:
        form = EditEntryForm(instance=entry)
    return render(request, '/edit.html', {'form':form})

def delete(request, id, slug):
    Entry.objects.filter(id=id, slug=slug).delete()
    return redirect('home')
urls.py

path('add_entry/', views.add, name='add'),
path('update/<int:id>/', views.update, name='update'),    
path('delete/<int:id>/<slug:slug>/', views.delete, name='delete'),

<a href="{% url 'detail' todayDiary.authuser_id todayDiary.slug %}">{{ entry.title }}</a>
<a href="{% url 'update' todayDiary.authuser_id %}">Update</a>
<a href="{% url 'delete' todayDiary.authuser_id todayDiary.slug %}">Delete</a>

and render form in templates as {{ form.as_p }} and it's good idea to seprate path for detail and delete don't mix it. Hope this helps.

You have specified the same path for both the urls detail and delete so change there :

 path('detail/<int:authuser_id>/<slug:slug>', views.detail, name='detail'),
 path('delete/<int:authuser_id>/<slug:slug>', views.delete, name='delete'),

And now in the template

 <a href="{% url 'delete' todayDiary.id todayDiary.slug %}">
Related