How to delete row when use Django only templates

Hello,
I tried several options but to no avail. How can I get the job done if I only use templates? I want to delete the selected record with a button. I also have an add button in the html, after the add button it shows me the base again.
I tried to add a separate path(#), as shown in the code, I get an error: __ Expected a Response , HttpResponse or HttpStreamingResponse to be returned from the view, but received a <class 'NoneType'>.

use:
Django only templates.
Django decorator @api_view.

All entries come with a delete button, but I can’t get the job done.

urls.py:

urlpatterns = [
...
    path("save", views.testing, name='save'),
    #path("save/<int:pk>", views.delete, name='delete'),
]

views.py:

@api_view(['GET', 'POST'])
def testing(request):
    mydata = models.FlatModelSqlite.objects.all()
    serializer_class = serializers.FlatSerializerSqlite(mydata, many=True)

    if request.method == "GET":

        html_save = "MyHome/save.html"
        template = loader.get_template(html_save)
        context = {
            'mymembers': serializer_class.data, 'flatP': ForecastField_all(),
        }
        return HttpResponse(template.render(context, request))

    elif request.method == "POST":
        item_serializer = serializers.FlatSerializerSqlite(data=request.data)

        if item_serializer.is_valid():
            item_serializer.save()
            return HttpResponseRedirect(redirect_to='save')
        else:
            er = item_serializer.errors
            html_save = "MyHome/save.html"
            template = loader.get_template(html_save)
            context = {
                'mymembers': serializer_class.data, 'flatP': ForecastField_all(),
                'errors': er,
            }
            return HttpResponse(template.render(context, request))


#@api_view(['GET', 'POST'])
#def delete(request, pk):
#    if request.method == 'POST':
#        flat_delete = models.FlatModelSqlite.objects.get(pk=pk)
#        flat_delete.delete()
#        return HttpResponse(redirect_to='save')

html /save:

{% for x in mymembers %}
    <tr>

      <td>{{ x.id }}</td>
        <td>{{ x.Address_N }}</td>
      <td>{{ x.Project_N }}</td>
      <td>{{ x.Fasi }}</td>
      <td>{{ x.Status }}</td>
      <td>
          <form method="post">
          {% csrf_token %}
          <input type="submit" value="Delete flat">
          </form></td>

    </tr>

  {% endfor %}

Given that @api_view is from Django Rest Framework and not just Django, there is some confusion going on.

Also, your using api_view but returning HTML which is not typical. If you want to return HTML you do not need to use @api_view.

I would recommend looking up the render method in the shortcuts module of Django. This will simplify your view code.

1 Like

As I understand I need apiview when I use react.js or other.
Especially when I need apiview or other decorator can you tell me?

Well, are you building a separate frontend? Or are you using Django Templates for your whole site?

I removed the decorator(@api_view) and used: return HttpResponseRedirect(redirect_to=‘/MyHome/save’) , now it works.