Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django javascript files

In a Django app, where should I put my javascript/jquery scripts?

like image 365
irl_irl Avatar asked Oct 07 '09 16:10

irl_irl


People also ask

Where does Django store JavaScript files?

Adding JavaScript to Our Django Template js file, put it in a js folder that you need to create in the static folder of your application. We use the static template tag to specify the relative URL to the JS file.

How do I run a JavaScript file in Django?

To load JavaScript file, just add the following line of code in index. html file. Run the server by using python manage.py runserver command. After that access the template by localhost:8000/index URL, and it will produce the following output to the browser.

Can I use JavaScript in Django?

While most of Django core is Python, the admin and gis contrib apps contain JavaScript code. Please follow these coding standards when writing JavaScript code for inclusion in Django.

What are static files in Django?

django. contrib. staticfiles collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production. For an introduction to the static files app and some usage examples, see How to manage static files (e.g. images, JavaScript, CSS).


2 Answers

In with your other static media. See here for more info:

http://docs.djangoproject.com/en/dev/howto/static-files/

like image 162
Paul McMillan Avatar answered Oct 23 '22 04:10

Paul McMillan


You have defined paths for media and static into the settings.py:

import os.path
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'media'))
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'static'))
  • Used BASE_DIR to calculate the path to the settings.py file
  • Used BASE_DIR to generate the MEDIA_ROOT and STATIC_ROOT paths to the previous path of settings

Following that structure the path structure should be:

-yourApp
----manage.py
----media
----static
----yourApp
--------settings.py
--------models.py

Django: How to manage static files

like image 2
AlvaroAV Avatar answered Oct 23 '22 03:10

AlvaroAV