How to set content type of JavaScript files in Django

django static files
javascript set content-type
django javascript
django static files not found
django static files not working
django static files 404
django static files not found in production
django can't find static files

I have a Django application, which requires several JavaScript files.

In Chrome I get the error "Resource interpreted as Script, but transferred with MIME type text/html".

AFAIK (see 2) in order to fix this problem, I need to configure Django so that JavaScript files are returned with content-type "application/x-javascript".

How can I do this in Django?

UPDATE: I followed the advice by Daniel Roseman and found following solution.

1) Modify urls.py:

urlpatterns = patterns('',
    ...
    url(r'.*\.js$', java_script),
    ...
)

2) Add following function to views.py:

def java_script(request):
    filename = request.path.strip("/")
    data = open(filename, "rb").read()
    return HttpResponse(data, mimetype="application/x-javascript")

I suspect the problem is not what you think it is. What is probably actually happening is that your JS files are not being served at all: instead, the Django error page is being sent. You need to figure out why.

JavaScript | Django documentation, Websites generally need to serve additional files such as images, JavaScript, or CSS. In your settings file, define STATIC_URL , for example: application and assumes the static content has already been collected under STATIC_ROOT . Managing static files (e.g. images, JavaScript, CSS)¶ Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”. Django provides django.contrib.staticfiles to help you manage them. This page describes how you can serve these static files.


I had an issue with Django serving javascript files as text/plain with the included server, which doesn't work too well with ES6 modules. I found out here that you could change file extension associations by placing the following lines in your settings.py:

#settings.py
if DEBUG:
    import mimetypes
    mimetypes.add_type("application/javascript", ".js", True)

and javascript files were now served as application/javascript.

Difference between application/x-javascript and text/javascript , <link href="http://static.example.com/pretty.css" type="text/css" media="screen" rel​="stylesheet"> <link A tuple describing the required JavaScript files. See the  Django and css/javascript files¶ Tags: django. How to bundle css/javascript files with your django applications? They’re an integral part of an application and should be versioned and bundled together with the app. Solution: django-staticfiles.


Since this doesn't prevent scripts from being interpreted correctly by the browser, why is this a problem? runserver is only for development (not production use), and as such is not a full blown web server.

You should continue to use it in development and when you move to production configure your webserver appropriately for static files.

However, if you absolutely must use the development server to serve static files; see how to serve static files.

Working with Django Templates & Static Files ― Scotch.io, additional files — such as images, JavaScript, or CSS — necessary to render the complete First, create a directory called static in your polls directory. load static %} <link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">. If you use django.contrib.staticfiles (make sure this app is added in your settings file!) and are running with DEBUG=True in your settings file , then django will automatically find and serve the static files found in the /static/ folder of each app. It will not find any static files who are not in an app folder.


For Django use request context in views :

return render_to_response('success.html', {'object': varobject},context_instance=RequestContext(request))

Managing static files (e.g. images, JavaScript, CSS), editorconfig file. We recommend using a text editor with EditorConfig support to avoid indentation and whitespace issues. Most of the JavaScript files use 4 spaces  Most of the JavaScript files use 4 spaces for indentation, but there are some exceptions. When naming variables, use camelCase instead of underscore_case. Different JavaScript files sometimes use a different code style. Please try to conform to the code style of each file.


Form Assets (the Media class) | Django documentation, see Managing static files (e.g. images, JavaScript, CSS). When you use a FileField or ImageField , Django provides a set of APIs you can use to deal with that file. default_storage.save('path/to/file', ContentFile(b'new content')) >>> path  Django File (and Image) Uploads Tutorial. Feb 13, 2020; This tutorial shows how to implement file and then image uploading with Django. We'll build a basic Instagram clone. Setup. If you're on a Mac the Desktop is a convenient place to put our code. The location doesn't matter; it just needs to be easily available.


Part 6: Static files, This is a security measure to prevent HTTP Host header attacks, which are If this is set to True , client-side JavaScript will not be able to access the CSRF The simplest possible settings file is for a single-database setup using SQLite. In this tutorial you will learn the concepts behind Django file upload and how to handle file upload using model forms. In the end of this post you will find the source code of the examples I used so you can try and explore.


Managing files | Django documentation, django-javascript-settings is a Django application that provides a way of Project description; Project details; Release history; Download files load javascript_settings_tags %} <script type="text/javascript">{% javascript_settings %}</script>  Create a folder templates. Declare the directory [os.path.join (BASE_DIR, 'templates')] in settings.py. Create a basic HTML page under templates. If you have static resources, such as CSS files, JavaScript files, image files and so on, declare the static resources path in settings.py.