Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Django views.py to return and execute javascript

So I'm working with django and file uploads and I need a javascript function to execute after the file has been uploaded. I have a file upload handler in my views.py which looks like this:

def upload_file(request):   
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():        
        for f in request.FILES.getlist('fileAttachments'):     
            handle_uploaded_file(f)
        return HttpJavascriptResponse('parent.Response_OK();')
    else:
        return HttpResponse("Failed to upload attachment.")

And I found a django snippet from http://djangosnippets.org/snippets/341/ and I put the HttpJavascriptResponse class in my views.py code. It looks as follows:

class HttpJavascriptResponse(HttpResponse):
    def __init__(self,content):
       HttpResponse.__init__(self,content,mimetype="text/javascript")

However, when I upload a file the browser simple renders "parent.Response_OK();" on the screen instead of actually executing the javascript. And Chrome gives me the warning: "Resource interpreted as Document but transferred with MIME type text/javascript"

Is there anyway to get views.py to execute the script?

like image 956
Alexandra Avatar asked Aug 06 '11 01:08

Alexandra


People also ask

Can I use Django and Javascript together?

Talking about the future web apps, Django has a lot to offer and has the ability to accommodate any modern web apps structures. Stacking Django and Javascript web frameworks to build modern web apps is one of the best way to stack backend and frontend frameworks.

What Does views PY do in Django?

In the Django framework, views are Python functions or classes that receive a web request and return a web response. The response can be a simple HTTP response, an HTML template response, or an HTTP redirect response that redirects a user to another page.

What are views PY?

A view function, or view for short, is a Python function that takes a web request and returns a web response. This response can be the HTML contents of a web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really.

How many views are there in Django?

Django has two types of views; function-based views (FBVs), and class-based views (CBVs).


2 Answers

A better way is to pass the mime to the HttpResponse Object.

See documentation: https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.HttpRequest.content_type.

    return HttpResponse("parent.Response_OK()", content_type="application/x-javascript")

Note - Some previous versions of Django used mimetype instead of content_type:

    return HttpResponse("parent.Response_OK()", mimetype="application/x-javascript")
like image 112
Claudemiro Avatar answered Sep 20 '22 06:09

Claudemiro


I believe this will work.

return HttpResponse("<script>parent.Response_OK();</script>")

However, you might think about returning a success (200) status code in this case, and then having some javascript in parent attach to the load event of this child, and branch based on return status code. That way you have a separation of view rendering code and view behavior code.

like image 30
Chase Seibert Avatar answered Sep 23 '22 06:09

Chase Seibert