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?
Related
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'm trying to extend django's admin change_form.html (django/contrib/admin/templates/admin/change_form.html).
What makes this more complicated for me is that I've installed grappelli, which also extends it (grappelli/templates/admin/change_form.html)
Now, I want to change it in myproject (to apply for all apps/modules in my project), and have tried to place change_form in various places but to no avail:
myproject/templates/admin/change_form.html
myproject/templates/grappelli/change_form.html
myproject/templates/admin/grappelli/change_form.html
Does anyone have a clue about where I should be placing my modified version of change_form.html in order for django to actually use it?
(any help on understanding django's search path & template extension mechanism will be appreciated).
Thanks!
You can include your template directory to TEMPLATE_DIRS
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'myapp/templates/'),
)
This way you can change the order in which Django reads your template files.
I have a django model class with several images related to each instance.
Those images follow a certain pattern and can be determined by the name field of the model.
Those Images reside within the project static files folder.
So I have written a method for my model class to generate file paths for the images. It searches the static files folder for all files that follow the pattern *.jpg (the asterisks is necessary, because the filename has incrementing numbers).
Once it has found a file it transforms the absolute filesystem path into an url that is passed to a view and template via a list.
def getImages(self)
matches[]
for filename in fnmatch.filter(
os.listdir(os.path.join(settings.STATIC_ROOT_DIR,'images')), self.name + '*.jpg'):
matches.append(
os.path.join(settings.STATIC_URL, 'images', os.path.split(filename)[1]))
return matches
This method works fine, but doesn't leave me quite satisfied. Here are the reasons:
For development mode I am required to introduce a new variable called STATIC_ROOT_DIR, to obtain the path of the static files folder. I would like to use a consistent way to reference the static root folder for development and production. How can I achieve this? I would like to avoid development mode hackery as much as possible.
I have to build a URL by joining the static_url path with other strings that will eventually make up the URL for this static file. Is there a better way to construct URLs? Maybe some library function?
Last but not least: Is it good practice to do this in a model? Or is such a task better be done by a view?
There is a STATIC_ROOT variable in settings.py. Why not use it?
Personally, I follow your way - just concatenating paths. But just found a function for that:
from django.contrib.staticfiles.templatetags.staticfiles import static
print static('yourfile.jpg')
It works for me.
I think model is a good place for it. You store files in filesystem like you store model data in database. In other words, both of these are examples of storage which is a model level thing.
I would like to throw in another one:
Using STATIC_ROOT will break if you host your files externally.
You can use the django-storage-backend yourself (untested, just written):
from django.core.files.storage import get_storage_class
from django.conf import settings
def getImages(self)
static_storage = get_storage_class(settings.STATICFILES_STORAGE)()
directories, files = static_storage.listdir('images')
return [
static_storage.url('images/' + file)
for file in files
if file.startswith(self.name) and file.endswith('.jpg')
]
This will even return the correct URL if you use CachedStaticFileStorage or S3BotoStorage (from django-storages). And this will also be fine if you are in dev-mode.
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.
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.