I’m new to Django and I’m trying to upload Form’s items in my template with AJAX call to avoid refresh.
Thanks to many Questions/answers here I have the following code which is working fine and renders the form as a table.
I would like to do the same thing BUT instead of rendering it as a table, I would like to be able to choose which item to display and use it like this {{ form.item_name }}
so I can organise my template like I want.
What should I change ?
view.py
def test_view(request):
if request.user.is_authenticated:
my_id = request.GET.get('id')
my_instance = models_companies.objects.get(id_company=my_id)
my_form = forms_companies(instance=my_instance)
return HttpResponse(my_form.as_table())
else:
messages.success(request, "You Must Be Logged In...")
return redirect('home')
template.html
function load_company_test() {
$.ajaxSetup({
data: {
id : $('#id_id_company').val(),
csrfmiddlewaretoken: '{{ csrf_token }}'
},
});
$.ajax({
type: "GET",
url: "../test_view/",
success: function (data) {
$("#div_company").empty();
$("#div_company").html(data);
},
});
}
// I want to populate the div like this
<div id="div_company">{{ data.item_name }}</div>