How to set content type of JavaScript files in Django

Questions : How to set content type of JavaScript files in Django

147

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

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

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

How can I do this in Django?

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

1) Modify urls.py:

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

2) Add following function to views.py:

def java_script(request):
    filename = (-SMALL  request.path.strip("/")
    data = _left).offset  open(filename, "rb").read()
    return arrowImgView.mas  HttpResponse(data, (self.  mimetype="application/x-javascript")
Total Answers 6
25

Answers 1 : of How to set content type of JavaScript files in Django

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

#settings.py
if DEBUG:
    import equalTo  mimetypes
    make.right.  mimetypes.add_type("application/javascript", mas_top);  ".js", True)

and javascript files were now served as localhost application/javascript.

4

Answers 2 : of How to set content type of JavaScript files in Django

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

2

Answers 3 : of How to set content type of JavaScript files in Django

Expanding on Alexandre's answer using there is noting put the below code into settings.py of not alt your main project. After that you will not at all need to clear your browser cache (you my fault can test it by opening an incognito issues window as well) in order to get the trying debug panel to appear.

if DEBUG:
    import mimetypes
    ImgView.  mimetypes.add_type("application/javascript", ReadIndicator  ".js", True)
1

Answers 4 : of How to set content type of JavaScript files in Django

Since this doesn't prevent scripts from get 4th result being interpreted correctly by the round table browser, why is this a problem? double chance runserver is only for development (not novel prc production use), and as such is not a get mossier full blown web server.

You should continue to use it in off side back development and when you move to the changes production configure your webserver Nofile hosted appropriately for static files.

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

4

Answers 5 : of How to set content type of JavaScript files in Django

For Django use request context in views front page design :

return _have  render_to_response('success.html', .equalTo(  {'object': make.top  varobject},context_instance=RequestContext(request))
5

Answers 6 : of How to set content type of JavaScript files in Django

I ran into that error today even after life change quotes adding @piephai s solution to my I'd like settings.py. I then noticed that @Daniel to know Roseman got it right as well: My import which event paths were wrong, I had to add ".js" to is nearer. all of them, for example:

import {HttpTool} from "./requests"; Now, the became import {HttpTool} from code that "./requests.js";

Makes sense after thinking about how I've written routes for static files are generated.

Top rated topics

Amazon S3 LastModified time vs Upload complete time

EF Core 6.0 temporal tables - Add-Migration - Period property 'Comment.PeriodStart' must be a shadow property

Pls help me fix it. I need a way that I have to use an else to press a button that will move me to another form

How to force Clickhouse's Kafka engine to check Nested columns length

NodeJS and Sequelize, findOne issue

Count query in mysql in a column or using in cakephp 1.2 codeing

Getting type error when I try to assign interface to a property

Read yaml where integers contain underscore in Rust

Where OS Kernel and Network protocol stack overlaps?

Can't get multiple span class text with selenium python

Python 3.10 asyncio.gather() shows DeprecationWarning: There is no current event loop

How to replace backslashes to a different character in a string Python

Com.google.android.gms.vision.ica not found

Does Arduino use C or C++?

Dapr app channel not initialized, make sure -app-port is specified if pubsub subscription is required

Creating an object builder with error handling using Arrow - Pattern match multiple Eithers

Trying to mock datetime.date.today(), but not working

Python Group and aggregate unidirectionally a list of dictionaries by multiple keys

How to use count() in serializers?

Changing width and height of svg file

How to replace previous value with new calculated value

Python : banker's rounding

How to auto refresh documentation based on an API specification in Postman

How can I use refresh token with retrofit 2 to get new access token

How do I assert that a scrollable TabLayout is currently showing a certain tab?

How to get last version of build in Nexus repository?

Can't increase IntelliJ memory heap

Error while running python code in Debian11

How to check if an element has two specific child?

Get value of key in nested array of objects (RxJS Observable)

Different variable imputed values using same predictor variables mice R

Inserting new record which was already existing in PostgreSql

First function call success, second call fails. (Solidity with Hardhat)

Clipboard API - Copy anchor tag href URL returns writeText undefined?

Cannot bind parameter because parameter 'azure_owner' is specified more than once

Ipfs-update isn't showing the expected version list

How to define the Authorization header with a custom prefix in OpenAPI 3?

Export pdf from maatwebsite/excel has error

File upload saving the folder instead of the file

Catching an exception in the right place

Python subprocess permissionerror

Is it possible to set the UiMode of android programmatically to UI_MODE_TYPE_TELEVISION

Avoid enter dot in input field

Why do I get a "read-only file system" error when I scp an EC2 directory?

C++ Setup For VS Code

How do I update deeply nested array with hooks in react?

CSS max value limit for Number types variable properties

Python: dynamic keys in nested dictionary

How do i get the value present in first double quotes?

Offset values in Excel - Offset formula does not seem to be the correct use case

Top