Django Delete View failiing with ManytoManyfield object

I am a Newbie, was trying to follow a tutorial and it seems examples provided don’t work well. My issue lies with an associative table which list skills for a given user profile. One user can have many skills. my add/edit functionality works well, the problem lies in the delete of a skill. the profile model has a field called skills with a manytomanyfield property

URL Path: path(‘delete-skill/str:pk/’, views.deleteSkill, name=‘delete-skill’)

View Function:

@login_required(login_url=‘login’)
def deleteSkill(request, pk):
print('Skill Primary Key == ', pk)
profile = request.user.profile
skill = profile.skills.get(id=pk)
print(‘Skill Query Value’, skill.id)

if request.method == 'POST':
    skill.delete()
    messages.success(request, 'Skill has been removed from your profile!')
    return redirect('account')

context = {'object': skill}
return render(request, 'delete-object.html', context)

This is the error:

Page not found (404)
The current path, delete-skill/7055d0f6-1736-44cb-86bf-92eb98c8159b/' , didn’t match any of these.

If I look in the users_skill and the users_profile_skill(associative table) I find the skill_id in question.

The input is type=‘submit’ and method=‘POST’

The confirm Delete page displays the view path correctly and the pk being passed.

http://localhost:8000/delete-skill/7055d0f6-1736-44cb-86bf-92eb98c8159b/

In the Django Console I see several GET requests, but don’t understand where they are originating from

Skill Primary Key == 7055d0f6-1736-44cb-86bf-92eb98c8159b
Skill Query Value 7055d0f6-1736-44cb-86bf-92eb98c8159b
[13/May/2022 16:04:50] “GET /delete-skill/7055d0f6-1736-44cb-86bf-92eb98c8159b/ HTTP/1.1” 200 2466
[13/May/2022 16:04:50] “GET /static/js/main.js HTTP/1.1” 404 1890
Not Found: /delete-skill/7055d0f6-1736-44cb-86bf-92eb98c8159b/’
[13/May/2022 16:06:05] “POST /delete-skill/7055d0f6-1736-44cb-86bf-92eb98c8159b/’ HTTP/1.1” 404 4401

I have read thru many blogs, the Django documentation on many to many object reference, but I am not finding a clear example of a delete of an individual record from an associative table.

Thanks for looking.

Side note: When posting code (or error messages, or logs, etc) enclose each block between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code (or messages, etc) properly formatted.

When you want to remove an association between to models in a ManyToMany relationship, you use the remove() function to do that, not .delete()
See the examples at Many-to-many relationships | Django documentation | Django

Hello Ken,

Thanks for the tip. I was just following the teacher in the tutorial, I did try changing the code from delete to remove() when reviewing the Django docs, but also got the same result. Something in the lesson resulted in all delete functions now failing within in each application :frowning: I will role back my code and try to get back to at least one application having a functioning delete view.

Thanks,

Fausto

Well after a week of banging my head with a Django 404 error it turned out the issue was an anchor tag on my delete-object.html form that was causing my grief! Once removed from the ‘’‘button’’ element everything worked! Thanks to Crazyzubr on stackoverflow for insight!!