Error when sending delete by ajax - Django Rest framework - ajax

I have 2 serializers:
class DetalleSerializer(serializers.ModelSerializer):
producto = serializers.CharField(source='producto.nombre')
class Meta:
model = DetalleVenta
fields = ('cantidad','producto')
class PedidoSerializer(serializers.ModelSerializer):
detalleventa = DetalleSerializer(many=True, read_only=True)
class Meta:
model = Venta
fields = ('id','cliente','descripcion','detalleventa','atendido')
and my viewset:
class PedidoViewSet(viewsets.ModelViewSet):
queryset = Venta.objects.exclude(atendido=True)
serializer_class = PedidoSerializer
def destroy(self, request, pk=None):
try:
queryset = Venta.objects.exclude(atendito=True)
object = get_object_or_404(queryset, pk=pk)
object.atendido = True
object.save(update_fields=['atendido'])
return Response({"status": True, "results": "Pedido atendido correctamente"})
except NotFound as err:
return Response({"status": False, "error_description": err.detail})
To remove simply change the state of my attended field, which is a Boolean (true / false) logical deletion.
y estas mis 2 urls:
url(r'^pedido/$',PedidoViewSet.as_view({'get': 'list', 'post': 'create'}),name='api-pedido',),
url(r'^pedido/(?P<pk>\d+)/$',PedidoViewSet.as_view({'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'}),
name='api-atendido',),
The recovery of all data is not a problem, it brings me everything I need.
via the url: url: "{% url 'api-pedido'%}", GET
But when I want to do the logical deletion of a button **(DELETE):
$('.btn').click(function(){
$.ajax({
url: "{% url 'api-atendido' %}",
data: {pk:91},
type: 'DELETE',
contentType: 'application/json',
success: function(result) {
console.log('atendido correctamente');
},
});
});
It shows me the following error: Reverse for 'api-atendido' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['pedido/(?P<pk>\\d+)/$']
error captures:
Something missing? or something am I doing wrong?

Issue is due to your url name. Your ajax url is {% url 'api-atendido' %}. That url name required a valid pk rather than sending pk as data. ajax url should be {% url 'api-atendido' 'pk' %}, where pk is the primary key of the model Venta.
$('.btn').click(function(){
$.ajax({
url: "{% url 'api-atendido' 91 %}",
data: {},
type: 'DELETE',
contentType: 'application/json',
success: function(result) {
console.log('atendido correctamente');
},
});
});
If you are calling the ajax call dynamically, then give the exact url instead of its name. Because template rendering is done by server. So all the template tags are converted during the page rendering. To do it dynamically consider the following code.
$('.btn').click(function(){
var pk = 91; //replace this with the actual id
$.ajax({
url: "pedido/"+pk+"/",
data: {},
type: 'DELETE',
contentType: 'application/json',
success: function(result) {
console.log('atendido correctamente');
},
});
});

Related

Ajax in Django : url not found

I am coding a small Django project where an user can select an object and save it in a database. I am trying to implement an Ajax call on a button to delete this object if necessary.
I am doing it step by step, debugging with the console.
my urls:
app_name = 'register'
urlpatterns = [
path('<int:user_id>/', views.account, name='account'),
path('/delete/', views.delete, name='delete'),
]
my view.py:
def delete(request):
data = {'success': False}
if request.method=='POST':
product = request.POST.get('product')
print(product)
data['success'] = True
return JsonResponse(data)
my ajax.js:
$("#form_id").on('submit', function(event) {
event.preventDefault();
var product = 'coca-cola'
console.log('ok till this point')
$.ajax({
url: '{% url "register/delete" %}',
type: "POST",
data:{
'product':product,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
datatype:'json',
success: function(data) {
if (data['success'])
console.log('working fine')
}
});
});
My view isn't doing much for now but I haven't any knowledge about Ajax and I am doing it one step at a time.
This is the error I get in the console:
jquery.min.js:2 POST http://127.0.0.1:8000/register/6/%7B%%20url%20%22register/delete%22%20%%7D 404 (Not Found)
As far as I understand, Ajax can't find my url: '{% url "register/delete" %}'.
I have tried '{% url "register:delete" %}' with no luck either.
I found an answer after some tweaking, I defined my url before the Ajax call and then pass it in it:
$("#form_id").on('submit', function(event) {
event.preventDefault();
var product = 'coca-cola'
var url = '/register/delete/'
console.log( url)
$.ajax({
url: url,
type: "POST",
data:{
'product':product,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
datatype:'json',
success: function(data) {
if (data['success'])
console.log('working fine')
}
});
});
Also you can add just the string of url to "url" parameter without characters {% url %}. Maybe you copied the code from pattern Django and added it to JS-file. So it does not work.

django ajax MultiValueDictKeyError

I am receiving this error:
MultiValueDictKeyError at /orders/ajax/add_order_line
"'cart'"
Here is my script
var cart = {
0: {
id: "1",
quantity: 50
}
}
$.ajax({
url: myURL,
type: "post",
data: {cart: cart},
success: function() {},
error: function(){}
});
Meanwhile in my django views, the error was found in this line:
def something(request):
cart = request.POST['cart']
Use get method of multivaluedict
request.POST.get('cart')
Your data is a nested array, so you can't send it using the default default application/x-www-form-urlencoded content type.
You can send the data as json:
$.ajax({
url: myURL,
type: "post",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({cart: cart}),
success: function() {},
error: function(){}
});
Then in your view, you have to load the json string from request.body instead of using request.POST (which is for form-encoded data only).
import json
def my_view(reqest):
data = json.loads(request.body.decode('utf-8'))
cart = data.get('cart')

ajax post request to view

I am trying to make a simple ajax request to a view but keep having a not Found error for the given url:
Not Found: /myapp/start_session/1/scan_ports/
"POST /myapp/start_session/1/scan_ports/ HTTP/1.1" 404 5711
js
$(document).ready(function() {
$.ajax({
method: 'POST',
url: 'scan_ports/',
data: {'csrfmiddlewaretoken': '{{csrf_token}}'},
success: function (data) {
alert("OK!");
},
error: function (data) {
alert("NOT OK!");
}
});
});
url
url(r'^scan_ports/$',views.ScanPorts,name='scan_ports'),
view
#login_required
def ScanPorts(request):
user = request.user
if request.method == 'POST':
currentSetting = models.UserSetting.objects.filter(isCurrent=True)
if currentSetting.serialPort:
print("GOT IT!")
return HttpResponse('')
Is the ajax request not set properly?
Assuming you are in the "myapp" app, replace:
method: 'POST',
url: 'scan_ports/',
for this:
method: 'POST',
url: '/myapp/scan_ports/',
First check your urls, the url on which you posted is incorrect hence 404 not found error.
Try to define your url in your JS as: {% url 'scan_ports' %} which will search your urls with the name your provided in urls.py
In addition, this may not be a good approach to submit a form via ajax.
Your JS should be something like this:
$('.form-class-name').on('submit', function (e) {
e.preventDefault();
console.log("ajax is called");
$.ajax({
type: $(this).attr('method'),
url: "/url-name/",
data: $(this).serialize(),
dataType: 'json',
success: function(data) {
alert("success");
},
error: function(data) {
alert("Failure");
}
})
}
e.preventDefault() prevents the natural/default action, and prevents from submitting the form twice.
.serialize(), serializes the form data in a json format.
append a "/" before and after your action url.
Your view must return a dictionary as ajax deals with JSON format.
Edit your view like this:
if request.method == "POST":
currentSetting = models.UserSetting.objects.filter(isCurrent=True)
if currentSetting.serialPort:
print("GOT IT!")
a = {'data':'success'}
return HttpResponse(json.dumps(a))
This will return a dictionary required by the ajax.

Django AJAX post not working with CSRF

I have this ajax like vote on my article model:
#csrf_exempt
#login_required
def like(request):
args = {}
if request.method == 'POST':
user = request.POST.get('user')
lu= request.user
post_id = int(request.POST.get('post_id'))
try:
liked_article = Article.objects.get(id = post_id)
except:
liked_article = None
if ArticleLike.objects.filter(liker=lu.id, article=post_id).exists():
liked_article.likes -=1
liked_article.save()
ArticleLike.objects.filter(article=post_id, liker=lu.id).delete()
else:
liked_article.likes +=1
liked_article.save()
newliker = ArticleLike(article=post_id, liker=lu.id)
newliker.save()
args.update(csrf(request))
args['likes'] = str(liked_article.likes)
return render(request, 'ajax_like.html', args)
Here is the ajax snippet:
$(function(){
$('#like').click(function(){
$.ajax({
type: "POST",
url: "/article/like/",
data: {
'post_id': $(this).attr('name'),
'csrfmiddlewaretoken': '{{csrf_token}}'
},
success: likeSuccess,
dataType: 'html'
});
});
});
function likeSuccess(data, textStatus, jqXHR)
{
$('#like_count').html(data);
}
and ajax_lik.html
<p id="like_count">
{% if likes > 0 %}
{{likes}}
</p>
{% else %}
wow
</i>
{% endif %}
The view works perfectly well with #csrf_exempt decorator active, but with csrf, I get 403 Forbidden error.
I tried different tweaks but could not figure out how solve this, so I appreciate your help.
If you are using a #csrfexempt decorator then you don't need to send a csrf_token. BTW, csrf_token shouldn't be part of the POST data,
data: {
'post_id': $(this).attr('name'),
'csrfmiddlewaretoken': '{{csrf_token}}'
}
it should be in the request's header "X-CSRFToken".
Here a little example to set custom headers to an ajax request using jquery (docs here):
$.ajax({
url: "/article/like/",
data: { post_id: $(this).attr('name') },
type: "POST",
beforeSend: function(xhr){xhr.setRequestHeader('X-CSRFToken', "{{csrf_token}}");},
success: likeSuccess,
dataType: 'html'
});
You can see Django's docs for this here

Error when returning single json object after an ajax call in django

when i get multiple objects, it works fine but if i only get 1, it returns an error
views.py
def get_incident_personnel(request):
args = {}
store = Store.objects.get(store_id=request.POST['store'])
data = serializers.serialize('json', store)
return HttpResponse(data, mimetype='application/json')
and here is my ajax.js
$('#id_store').change(function() {
var store = $('#id_store').val();
$.ajax({
url: '/incidents/get_incident_personnel/',
type: 'POST',
dataType: 'json',
data: {
'store': store,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
success: function(data) {
console.log(data);
}
});
});
You can't use serializers.serialize on a model instance, only on a QuerySet. Use filter even though you know you're only going to get one object.
store = Store.objects.filter(...)

Resources