Django STATIC_URL = '/' [duplicate] - python

This question already has answers here:
Django: how to make STATIC_URL empty?
(2 answers)
Closed 8 years ago.
I would like to serve static files in my Django application directly from the root.
If i set:
STATIC_URL = '/static/'
all works as expected. However I would like to serve files from the '/' of my site without having to access the 'static' subfolder.
Is that possible? If I set:
STATIC_URL = '/'
I receive code 404 and this error:
Directory indexes are not allowed here.

just to make sure- you are not confusing the following:
STATIC_ROOT refers to where you put your static files
STATIC_URL refers to where queries over the internet will get your static assets (eg: www.abc.com/static/jquery.js)
if you do indeed mean STATIC_URL, then it sounds like it could cause a lot of trouble, because then your static files might have the same name as a url pattern that you have defined for one of the views.

Related

Changing the name of the sub folder inside Django Template path

I am new to Django and I have a path to my HTML file as
./templates/registration/login.html and I want to change it to
./templates/index/login.html
After renaming it to /templates/index/login.html, it is still picking up the old directory /templates/registration, could not find login.html screen and it throws an error.
Could someone tell me what changes we have to make in settings.py when we rename a folder name inside templates structure?
In your settings.py, you can define the templates location, which applies the template function per app.
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates')
)
I assume your app is named registration, hence why it is looking there. You can either rename your app or place all your templates in a global folder at the same level as the apps (and not within the apps themselves). The latter is not recommended for large applications.
Edit: Why is this labeled Python2? What version of Django is this?

django collectstatic: How to keep the project structure?

Currently, collecstatic gathers all files of the project's Apps in one directory (STATIC_ROOT).
What should be done to keep the project structure for the static files:
STATIC_ROOT/App1
..
STATIC_ROOT/Appn
STATIC_ROOT/App3
thanks
The documentation recommends namespacing static files:
Static file namespacing
Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.
Make sure to create an inner directory named after your app and put your files in there, e.g.
my_app/
static/
my_app/ ← This directory
file1
file1
...
Thank you chris,
I find exactly the solution as indicated here: How should I structure my static app files in Django?
I have just replace STATICDIR_FILES by :
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

Django Doesn't Serve STATIC_ROOT in DEBUG

I'm using Python 3.5 and Django 1.10 to run a development server:
./manage.py runserver 0.0.0.0:8000
In my settings.py I have:
DEBUG = True
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
And an app directory, with a static subdirectory for its static files:
proj/
proj/
...
app/
static/
a.txt
...
static/
b.txt
Pretty standard.
However: Django doesn't serve the STATIC_ROOT when DEBUG = True. It returns app/static/a.txt for /static/a.txt alright, but not static/b.txt for /static/b.txt.
Changing settings.py to read:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
Works - but then I have to comment out STATIC_ROOT (otherwise Django complains it can't be in STATICFILES_DIRS).
Now, I can't just "use a different external static directory", e.g. static2, because I'm using django-sass-processor, which compiles .sass files into .css files, and puts these .css files in the STATIC_ROOT (which, as I've said, is inaccessible).
Things I've tried:
Settings up NGINX to serve that directory (like in a production environment). Works, but there just has to be another way.
Configuring django-sass-processor to write the .css files into said "different external static directory", e.g. static2, and including it in STATICFILES_DIRS. Again, works, but it just can't be that complicated!
Manually adding static files URLs in urls.py:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
This one was quite a trip, so I thought I'd share it to save other people the trouble: it turns out that Django does that anyway, and actually overwrites my URLs - but it includes, as I've said, only my apps' static directories and those in STATICFILES_DIRS.
I even changed DEBUG to False and removed the if - but that didn't work as well, because then the django.conf.urls.static.static function actually returns an empty list. So, I implemented it myself using django.views.static.serve, and it finally worked, but again - it doesn't make sense I have to turn off debugging and manually implement serving static file.
Update
If you're working with django-sass-processor and running into similar problems, they've actually provided a solution I just noticed in their documentation: a special static finder you can add in your settings.py like so:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'sass_processor.finders.CssFinder',
]
(The first two are Django's default finders, so when you override this configuration you should include them manually).
But even now, for anything other than .css files, the STATIC_ROOT is actually the only static directory that is inaccessible via /static/, which I find pretty odd. So, I'd still like to solve (or at least understand...) it.
At first glance it seems very strange that Django doesn't serve files from STATIC_ROOT, but it makes sense when you consider the workflow Django intends for static files. Django expects that you bundle static files alongside apps and you save project-level static files in a separate directory that is checked into version control. Then, when you run manage.py collectstatic the STATICFILES_FINDERS are used to gather up all the files into a single directory (which should not be in version control) so they can be deployed to AWS S3 or whatever.
So the staticfiles finders have two jobs:
find a file via path, used for DEBUG mode static file serving
list all of the available files, used for collectstatic
If you were able to include STATIC_ROOT in your STATICFILES_DIRS then the collectstatic command would probably get stuck in a loop. Also, the Django developers don't intend for you to save files in STATIC_ROOT since it shouldn't be checked into version control.
However, there's times when you actually do want to serve files from STATIC_ROOT. In my case, I have a Celery task that creates thumbnails of uploaded images and saves them to the static files storage. In production that ends up saving on AWS S3 where it can be served.
So, if you have a valid use case for serving files from STATIC_ROOT just define your own file finder and add it's path to STATICFILES_FINDERS:
from django.contrib.staticfiles.finders import BaseFinder
from django.contrib.staticfiles.storage import staticfiles_storage
class StaticRootFinder(BaseFinder):
"""
For debug mode only. Serves files from STATIC_ROOT.
"""
def find(self, path, all=False):
full_path = staticfiles_storage.path(path)
if staticfiles_storage.exists(full_path):
return [full_path] if all else full_path
return []
def list(self, ignore_patterns):
return iter(())

Confused about configuring django-avatar for storing avatar

Edit: Some random edits seem to do the work, but I don't understand how.
I have just "installed" the app django-avatar into my django project. (currently, it is under development, so I am using the server which comes with django)
However. after I upload an avatar, neither can I find the avatar in any folder, nor is it being displayed. I am new to django, and python, so I don't know what to do, or which part of my code to post. I am completely clueless. I have set up a url, at which static files are server(via django.views.static.serve)
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
(settings is imported)
However. I feel this is wrong. I would like some guiding.
Media/Static related settings:
MEDIA_ROOT = 'd:/a4m/media/'
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (('d:/a4m/static/'),)
As for templates, I am just using the ones which django-avatar provides.
First, don't add your project's static directory to STATICFILES_DIRS. You shouldn't be manually putting anything in that directory, so there's no reason to serve it in development (which is why Django doesn't by default). Also, I don't think it's related to this issue, but STATIC_ROOT needs a real value, which should be in your case:
STATIC_ROOT = 'd:/a4m/static/'
That said, two things from looking at the django-avatar source:
It uses a setting named AVATAR_STORAGE_DIR, so you should probably define that if you haven't. It seems the following would be sensible:
AVATAR_STORAGE_DIR = MEDIA_ROOT + 'avatars/'
If, for whatever reason, the avatar can't be written to the filesystem, django-avatar returns silently. So, I would check things like permissions to ensure that Django will have no problems writing to any directory under AVATAR_STORAGE_DIR.
Hopefully, that will be enough to get you a further in debugging this issue.

What does Django do with `MEDIA_ROOT`?

What does Django do with MEDIA_ROOT exactly? I never understood it. Since Django itself doesn't serve static media, and you have to set up apache or something similar for it, why does it care in which directory it sits?
You're not the only one who wonders; check out Django ticket #10650. Based on the comments by Django developers there, I think this pretty much sums up what MEDIA_ROOT is used for:
Unfortunately, Django is also at fault for being far too vague in its docs about what
MEDIA_ROOT and MEDIA_URL are used for. Searching through Django's code confirms that
MEDIA_ROOT & MEDIA_URL are used for file upload, and that MEDIA_URL is provided as a
convenience to templates via the default context processors (available when using
RequestContext??).
It appears to be used by some classes in Django as a default -- for instance, FileSystemStorage uses it as the default place to store files.
When you creates a model with ImageField or FileField attributes, you should pass the upload_to argument. That is a relative path will be appended to your MEDIA_ROOT path and there will be save and retrieve that files.

Categories

Resources