Hi Steve,
I am new to Django as well. To do what you wanted to do (pass values from
python to js), I used ajax queries (using jquery) and then did a
JsonResponse to the ajax call. The ajax call then handles the data as
required.
Sample usage:
VIEWS.PY:
class InstructorDeleteView(DeleteView):
model = Instructor
success_url = reverse_lazy('instructor_list')
template_name = 'class_management/instructor_list.html'
def delete(self, request, *args, **kwargs):
"""
Calls the delete() method on the fetched object and then
responds to the call with the relevant data.
"""
self.object = self.get_object()
pk = self.object.pk # Save the primary key before the object is
deleted.
self.object.delete()
data = {
'message': "Successfully deleted the instructor.",
'pk': pk,
}
return JsonResponse(data)
Then, in you template, you can add js and have a ajax call to the view
above so that it can receive the JsonResponse:
TEMPLATE:
$.ajax({
method: "POST",
url: $id + "/delete/",
data: $data,
success: handleSuccess,
error: handleError,
});
function handleSuccess(data, textStatus, jqXHR) {
console.log("SUCCESSFULLY DELETED INSTRUCTOR.");
console.log("Removing the deleted entry from the table.");
$('#row' + data['pk']).remove();
console.log(data);
console.log(textStatus);
console.log(jqXHR);
}
function handleError(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown)
}
This was a POST request using AJAX and I am sure a GET request will work
fine as well.
Please note: There might be better alternatives out there. I just don't
know them yet :)
Best,
Akshay
On Wednesday, May 23, 2018 at 6:52:05 AM UTC-5, steve Valet wrote:
>
> I am trying to pass values from Django/Python to Javascript, and have it
> working to a degree, but need some assistance (obligatory: I'm new to
> Django so could be hosing this)
>
> I have three files in play here, *views.py, app.js, home.html*. I can get
> the values passed to home.html as part of the render, but I can't get the
> code to work outside of the home.html file. Below is a *very simplified*
> example of what I'm doing.
>
> *VIEWS.PY*
>
> return render(request, "home.html", {'data':data'})
>
>
> *HOME.HTML*
>
> <html>
>
> <head>
>
> <script src="{% static '/js/app.js' %}"></script>
>
> <script>
>
> myFunction({{ data | safe }});
>
> </script>
>
> </head>
>
> </html>
>
>
> *APP.JS*
>
> function *myFunction*(vars){
>
> return vars
>
> };
>
>
>
>
> I would like to have the JS in the HTML file in the APP.JS file so I can
> centralize all Javascript, but cannot get it to work outside the HTML. I also
> tried sending the "data" to JSON using "json.dumps" but got an error that the
> data was not serializable, so went with this code posted above.
>
>
> Can I do what I'm trying to do?
>
>
> Thanks
>
> Steve
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/8007b6f4-1ce6-4774-8fc0-81917e521204%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.