0

I had my static files working correctly until I tried to add this library https://github.com/ierror/django-js-reverse

I followed the instructions from the page, but Django still can't find the new resource.

from settings.py

STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static/'),
)


INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_js_reverse',
    ...,
)

in my template

<script src="{% static 'django_js_reverse/js/reverse.js' %}"></script>

my directory structure looks like this

staticfiles/
    admin/
    dist/
    django_js_reverse/
        js/
            reverse.js

After running

python manage.py collectstatic
python manage.py runserver

I also tried to run the following according to the library instructions, but no luck

./manage.py collectstatic_js_reverse

Here's the output from runserver when I try to load the page.

[08/Mar/2015 19:12:33] "GET /static/custom.css HTTP/1.1" 304 0
[08/Mar/2015 19:12:33] "GET /static/dist/css/bootstrap.min.css HTTP/1.1" 304 0
[08/Mar/2015 19:12:33] "GET /static/django_js_reverse/js/reverse.js HTTP/1.1" 404 1697
[08/Mar/2015 19:12:33] "GET /static/dist/js/bootstrap.min.js HTTP/1.1" 304 0
[08/Mar/2015 19:12:33] "GET /static/csrf.js HTTP/1.1" 304 0
[08/Mar/2015 19:12:33] "GET /static/custom.js HTTP/1.1" 304 0

I can't figure out where the issue is. The static files settings are obviously set up correctly because all the other files are being loaded fine. I must be missing something obvious. Any help would be appreciated\

UPDATE

I tried moving the reverse.js file to dist/ but it still returns a 404.

The permissions on all the files are the same

ls -la staticfiles/dist/js
-rw-r--r--  1 adam  staff  67155 Mar  8 15:06 bootstrap.js
-rw-r--r--  1 adam  staff  35601 Mar  8 15:06 bootstrap.min.js
-rw-r--r--  1 adam  staff    484 Mar  8 15:06 npm.js
-rw-r--r--  1 adam  staff   3258 Mar 10 10:31 reverse.js
6
  • collectstatic won't help if you are running runserver. Is staticfiles your STATIC_ROOT or one of STATICFILES_DIRS? Try copying the reverse.js file into dist, change the script src accordingly and check if that works.
    – Kedar
    Mar 9, 2015 at 16:28
  • Sometimes you might have problem with gif type of files. Try to chmod +x them :)
    – DAKZH
    Mar 9, 2015 at 20:22
  • Can you post your .gitignore?
    – erip
    Mar 10, 2015 at 14:36
  • Also, did you add urlpatterns = patterns('', url(r'^jsreverse/$', 'django_js_reverse.views.urls_js', name='js_reverse'), ) to your urls.py?
    – erip
    Mar 10, 2015 at 14:39
  • Yes, I tried that with the same results. I shouldn't have to do that if I'm using it as a static file though. If I add that url pattern and change my template to {% url 'js_reverse' %} then it works through the view. I still don't understand why the satic file isn't being found though.
    – adamF
    Mar 10, 2015 at 15:42

2 Answers 2

2

Got same problem and tried everything. Url to js file seems legit but it isn't (with Django 1.9 at least).

I've changed it from

<script src="{% static 'django_js_reverse/js/reverse.js' %}"></script>

to

<script src="{% url 'js_reverse' %}"></script>

And you need this url pattern in urls.py for this solution

# import view
from django_js_reverse.views import urls_js

# and add this in url patterns
url(r'^jsreverse/$', 'django_js_reverse.views.urls_js', name='js_reverse'),

And now it's working without any problem . Hope this will help somebody.

0

I've faced this same issue recently, here's how I solved it.

One possible solution is copying reverse.js into your static directory, in this case BASE_DIR/static/.

# set the JS-reverse output to the static dir
JS_REVERSE_OUTPUT_PATH=os.path.join(BASE_DIR, 'static/')

# update the script's path
<script src="{% static 'reverse.js' %}"></script>

Then:

./manage.py collectstatic_js_reverse
./manage.py runserver # or collectstatic

Another solution would be alternating between static file and views usage in the templates. Assuming that you've added the url, as explained in the Przemysław Melnarowicz's answer, and that SERVE_STATIC tells if static files are being served from django app (./manage.py runserver):

{% if SERVE_STATIC %}
    {# get script from view #}
    <script src="{% url 'js_reverse' %}"></script>
{% else %}
    {# get script from static file #}
    <script src="{% static 'django_js_reverse/js/reverse.js' %}"></script>
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.