Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Using Ajax to send form data to Django view

I want to use selected value from the template to filter data in months but keep getting 403 (Forbidden) error. Here are my files:

Models.py
class FuelOperation(models.Model):
date = models.DateField(default=datetime.date.today)
liter = models.IntegerField()

Template.html

<form id="month-form" method="post">
{% csrf_token %}
<select id="month-select" class="selectpicker">
<option value="03" name="March">3</option>
<option value="04" name="April">4</option>
</select>
</form>


Javascript

$("#month-select").on("change", function(){
var selectedValue = $(this).text();

$.ajax({
url : "{% url 'index' %}",
type : "POST",
data : {"value" : selectedValue},
dataType : "json",
success : function(){

}
});
});


views.py

def index(request):
if request.method == 'POST':
key = request.POST.get('value')
operations = FuelOperation.objects.filter(date__month=key)
context = {'operations': operations}
return HttpResponse(simplejson.dumps({"success": "true"}, mimetype="application/json"))

return render(request, 'fuel/index.html', context)

urls.py

path('', views.index, name='index'),



And I hope my code formatting appears good because it's my first
time posting so...

/r/django
https://redd.it/88f107