Field 'id' expected a number but got

后端 未结 3 627
鱼传尺愫
鱼传尺愫 2021-01-25 17:04

i want to delete and edit notes in my django app, but i am struck on this error for long Error : \"TypeError at /delete/1/ Field \'id\' expected a number but got .

3条回答
  •  长发绾君心
    2021-01-25 17:52

    You need to change here:

    from django.shortcuts import render
    
    def del_note(request, note_id):
    
        x = Note.objects.get(id = note_id)  # <-- Here
        print (x) // tried for degugging
        x.delete()
        return redirect("/")   
    
    
    def edit_note(request, note_id):
        x = Note.objects.get( id = note_id) # <-- Here
        form = NoteForm(request.POST or None, instance=x)
        if request.method == "POST":
            if form.is_valid():
                form.save()
                return redirect("/")
        return render(request, 'edit_template.html', context={'form':form,'sticky':x})
    
     # edit_template.html
     
    {% csrf_token %}
    {{ form.as_p }}

    The error was occurring because you passed id, but you should pass note_id instead.

提交回复
热议问题