I finally got around to trying out django-devserver. It's installed and working but static files refuse to load (Django 1.3). (Note: static files work fine with the standard runserver management command, before switching to django-devserver, so all the configuration settings are fine.) I came across this bit in the README:
DEVSERVER_IGNORED_PREFIXES = ['/media', '/uploads']
A list of prefixes
to surpress and skip process on. By default, ADMIN_MEDIA_PREFIX,
MEDIA_URL and STATIC_URL (for Django >= 1.3) will be ignored (assuming
MEDIA_URL and STATIC_URL is relative)
Which seems very odd because the whole point of using runserver is to not have to have an actual real web server setup, especially just to serve static files in development.
Oddly, though, even though it mentions ADMIN_MEDIA_PREFIX, I found that the admin actually loads all its static resources fine, which leads me to believe that maybe I'm just missing something somewhere.
Anyone ideas?
From the URL in #MarkLavin's comment, I actually came across (rather, reminded of) the following:
# Add to end of urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
That will allow the static files to be served and is ignored in production, so there doesn't seem to be any side effects to the approach. However, it irks me a little to have to modify my urls.py just for this, but it's probably only temporary until the noted pull request is merged.
If anyone has any other solutions, feel free to add them, though.
Related
When I run python manage.py collectstatic, it makes a copy of each image, JavaScript, and CSS file with a hash in the filename:
Post-processed 'css/theme.css' as 'css/theme.afeb1fc222a9.css'
Post-processed 'css/custom.css' as 'css/custom.585e1b29ff9a.css'
...
I'm assuming this is just a way of making a versioned filename for better caching; the client or CDN can be told to cache this file indefinitely, because if I make a change, the hash will differ, and I'll just reference the new version by the new name.
However, I'm not clear on how I'm supposed to reference this URL. The documentation on serving static files just says,
In your templates, either hardcode the url like /static/my_app/example.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files).
I went through my templates and dutifully switched every static resource (including the CSS files) from a hardcoded URL to a {% static "..." %} template tag, assuming it would map to the versioned filename where appropriate. But it doesn't.
I'm also using WhiteNoise for serving the resources, and I'm not entirely sure how it affects things, but it also says,
Want forever-cacheable files and compression support? Just add this to your settings.py: STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
But I have that in my settings file and it also doesn't seem to do anything with these versioned filenames.
If DEBUG is True then the static url will be 'css/theme.css' instead of
'css/theme.afeb1fc222a9.css'
If I define only one, the page displays without any CSS/JS. If I define both, the pages load fine. Why is that? I thought STATICFILES_DIRS is extraneous?
There's an answer here but it doesn't answer why I need both. It simply says what both does, which in my eyes is the same thing. What's the difference?
List of definitions for STATIC/STATIC_ROOT/STATICFILES_DIR
From what I have scoured on StackOverflow, I think I can sum it up as follows:
STATIC_ROOT is referenced as the static server, be it the default Django static server or the static server made on Heroku/etc. You point to this in urls.py
STATICFILES_DIR is referencing the stuff within static assets so if there are separate folders, one for CSS, one for JS, etc.
Feel free to add!
STATICFILES_DIRS is extraneous. You only need to use it if you are using a static files directory outside of the static files directories inside of your apps. Without seeing more of your code, and the structure of your project I can't even begin to guess why your pages aren't loading, but you definitely do not need to specify a STATICFILES_DIRS.
I am trying to debug this bizarre 404 error that surfaced in my Django application.
Page not found (404)
Request Method: GET
Request URL: http://78.198.124.245/
Directory indexes are not allowed here.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
What does "Directory indexes are not allowed here." mean? What is a directory index?
I Googled around, and the results I found all have to do with serving static files. However, I don't think I do that. What does this error mean?
Check your settings.py file for the STATIC_URL value. You want the value to be the subfolder where your static files are stored - generally STATIC_URL = '/static/'.
The answer depends on which django version you are using. For 1.4+ then just set the STATIC_URL
For 1.3.x its not so much what the STATIC_URL is set to as what your ADMIN_MEDIA_PREFIX is set to.
If you set it to /admin/ then django development server will attempt to serve static files for everything under /admin/ out of the contrib/admin/media/ folder
This means that http://127.0.0.0:8000/admin/postz/post/473 will attempt to find static content at django/contrib/admin/media/postz/post/473 and that's what the 404 is
If you are trying to access http://127.0.0.0:8000/admin/ then that would be an index.html inside of the admin media directory but the internal static server does not allow indexes so that's the error that it throws.
The accepted answer isn't exactly correct. Setting STATIC_URL may have worked as a side effect, but the real issue was that ADMIN_MEDIA_PREFIX was wrong.
The best settings would be:
ADMIN_MEDIA_PREFIX = '/media/'
or
ADMIN_MEDIA_PREFIX = '/admin/media/'
For 1.4 then just set the STATIC_URL as ADMIN_MEDIA_PREFIX is deprecated
https://docs.djangoproject.com/en/dev/releases/1.4/#django-contrib-admin
I had the same problem sometime ago and I also was looking for an answer but couldn't find anything useful. However, the problem for me was the the name I entered in the path in the urls file in my app didn't match the name of the function in views.
path('home-en', views.home_en, name="home")
Here 'home-en' should've been 'home_en' to match the name of the view function.
The reason for getting this error is that maybe trying to map our view with a custom url which luckily is a directory name.
so in this case, for e.g: we're storing our all static files in Static named folder and we're trying to create a url : path('static/',views.static_view).
So in this case it will not work.
what we can do, we can change the custom url to any generic url such as 'inex/' and it will work.
My static files were absolutely working fine, but yesterday I tried using the app allauth and since then my static files are not being displayed. It is not showing any css or js. I tried commenting the allauth app and even then it does not work.
However I just figured out the problem. Django is appending the wrong path to the static files. It should be /static/style.css but it is searching it at /"module-name"/style.css What could be the possible reason. I have correctly configured the static files path in the settings.py file as
STATIC_URL = "/static"
allauth prescribes using TEMPLATE_CONTEXT_PROCESSORS like this::
TEMPLATE_CONTEXT_PROCESSORS = (
...
"allauth.context_processors.allauth",
"allauth.account.context_processors.account"
)
Here, the dots refer to whatever context processors you were using before. If you did not have any, you were implicitly using the Django defaults specified over here: https://docs.djangoproject.com/en/1.3/ref/settings/#template-context-processors
So I suspect your problem will go away when you insert the defaults where the dots are. Correct?
I'm a complete newbie to apache, so the the whole deployment process has been a pain in the backside, so bear with me.
I'm having the problem of my admin site using the css formatting, it's just coming up as plain html.
I know there are other questions that answer this issue here and elsewhere, but for the life of me I can't figure out to get it working. So if anyone can spot what I'm messing up I'd be eternally grateful.
The relevant lines in settings.py are:
STATIC_ROOT = '/var/www/logparseradmin/logparser/static'
STATIC_URL = 'http://theyard/logparseradmin/static/'
ADMIN_MEDIA_PREFIX = '/var/www/Django-1.3.1/django/contrib/admin/media/'
and I have:
Alias /static/ /var/www/Django-1.3.1/django/contrib/admin/media/
in my httpd.conf.
I've tried a whole bunch of variations on this, as the various answers on the Internet have suggested, but no luck.
Thanks very much.
There's at least three things wrong here.
Firstly, your STATIC_URL is not probably a valid URL - http://theyard/ is not a real domain name, and won't work unless you've got local DNS resolution (which, given your self-described newbie status, seems unlikely.)
Secondly, the path value of STATIC_URL doesn't match the Alias you've put in httpd.conf - STATIC_URL has /logparseradmin/static/, whereas Alias just has /static/
Thirdly, ADMIN_MEDIA_PREFIX should be a URL path, not a file path.
And without seeing the rest of your http.conf it's impossible to be sure, but there may be fourth issue with mod_wsgi matching the URL before your Alias is processed.