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 %}