1

I'm a beginner in using django and tryin to make my first app, but I keep on getting "Not found" every time I add a javascript file on my view

This is my setting.py

STATIC_URL = '/home/me/PycharmProjects/GLife/static/'
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')

MEDIA_URL = '/media/'

STATIC_ROOT = 'static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, 'static'),
)

urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^register', include('register.urls')),
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),

]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!--<script src="/static/djangular/js/django-angular.min.js" type="text/javascript"></script>-->
    <script src="{% static '/js/_register/register.js' %}" type="text/javascript"></script>
    <title></title>
</head>
<body>

I've already search about it but the suggestions failed, hope anyone could help

here's my project structure

myproject
--main

--register
    ---migrations
    ---templates

--static
    ---js
        ----_register

I'm just trying to make an alert display as a testing using js

newbiemeh
  • 11
  • 2
  • possible duplicate of [Django Static Files results in 404](http://stackoverflow.com/questions/14799835/django-static-files-results-in-404) – Henrik Andersson Sep 05 '15 at 05:09
  • I removed STATIC_ROOT just like it said but still getting the same error – newbiemeh Sep 05 '15 at 06:34
  • Where exactly is `register.js` (and any other files that are giving you problems) located relative to `manage.py`? – Mike Covington Sep 05 '15 at 06:51
  • @mfcovington I've added my project structure on my question, I was just trying to diplay an alert for testing – newbiemeh Sep 05 '15 at 06:57
  • 1
    Shouldn't the `STATIC_ROOT` start with a slash: `'/static/'`? Also: are you running this in development mode (with the development server), or in another way? In the later case, you may need to run `manage.py collectstatic`. –  Sep 05 '15 at 07:26
  • yes it's on development phase and running it on a development server, I just made STATIC_ROOT='/static/' and run manage.py collectstatic, but still the same. Do I need to move some of my files? – newbiemeh Sep 05 '15 at 07:52
  • I got it, I just need to move my files to main folder inside myproject, thanks! – newbiemeh Sep 05 '15 at 08:56

1 Answers1

3

This is happening because you switched the values of STATIC_ROOT and STATIC_URL. It must be like this:

STATIC_URL =  '/static/'
STATIC_ROOT = '/home/me/PycharmProjects/GLife/static/'

STATIC_URL is the URL to use when referring to static files located in STATIC_ROOT.

STATIC_ROOT is the absolute path to the directory where collectstatic will collect static files for deployment.

Also, your static and STATIC_ROOT paths must be different.

When using the development server you don't need to configure STATIC_ROOT or use collectstatic management command because it will automatically serve the static files from static folder if DEBUG is true.

mehd azizi
  • 594
  • 1
  • 5
  • 16
almalki
  • 4,595
  • 26
  • 30