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

7
  • possible duplicate of Django Static Files results in 404 Commented Sep 5, 2015 at 5:09
  • I removed STATIC_ROOT just like it said but still getting the same error Commented Sep 5, 2015 at 6:34
  • Where exactly is register.js (and any other files that are giving you problems) located relative to manage.py? Commented Sep 5, 2015 at 6:51
  • @mfcovington I've added my project structure on my question, I was just trying to diplay an alert for testing Commented Sep 5, 2015 at 6: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. Commented Sep 5, 2015 at 7:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.