What is the use of STATIC_URL - django

Regarding static files. i was able to understand the following:
STATIC_ROOT - files where the static files would be copied op collectstatic command
STATIC_DIR- list where static files live.
Now what is the use of STATIC_URL, Read through a lot of resources, not able to understand it.
Can any one please explain ?

The settings mean quite different things.
As you've said, STATICFILES_DIRS and STATIC_ROOT (there's no Django setting called "STATIC_DIR") refer to locations on disk. STATIC_URL - as the name implies - refers to the URL that these files should be served under.
In development, Django will automatically serve files from STATIC_ROOT at STATIC_URL. In production, it's up to you to configure your server to do so.

STATIC_DIR is where additional static files live, STATIC_URL is where static files live.
Basically STATIC_URL is necessary, where STATIC_DIR is not but can help clarify addition (such as template static files) locations.

Related

Why Django 4.0 can access static files only with STATICFILES_DIRS, not STATIC_ROOT

When I use STATIC_ROOT:
STATIC_ROOT = BASE_DIR / 'static'
Django cannot access static files and always return 404 error for them.
But when I use STATICFILES_DIRS:
STATICFILES_DIRS = [
BASE_DIR / "static",
]
Everything works well. What's the issue?
I don't understand why Django works in this way, I always thought that STATIC_ROOT is correct way of creating route.
STATIC_ROOT is where the collectstatic command will copy all the static files in.
This folder is meant for production use. So that all the static files in your project (from your apps, or third party apps etc.) are kept in a single folder and therefore becomes easy to serve in production.
By default, Django automatically searches for static files inside each app directory (this is helpful in writing reusable apps). The STATICFILES_DIRS setting allows you to define extra places to look for the static files.
Also, the STATIC_ROOT folder and the STATICFILES_DIRS folder should not be the same values.

Django: Is "collectstatic" supposed to collect media files as well?

I dont know if I am confusing the purpose of collectstatic. Here's my settings module:
# BASE_DIR is the location of my django project folder
STATIC_URL = '/this.static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "ve_static_root")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "this.static"),
os.path.join(BASE_DIR, "this.media"),
)
MEDIA_URL = '/this.media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "ve_media_root")
Here, I placed both my static files and media files in STATICFILES_DIRS so that I can use media files as an easy reference when I recall/embed images into my markdown documents, but as you can also see, I made a path for MEDIA_ROOT, which needs to be different from STATIC_ROOT. My concern is that I placed my media folder, this.media, in the STATICFILES_DIRS folder, which allows me to call the images or videos into the Django templates using these staticfile filters, like {% static 'image/file/path/here' %}. As convenient as that is, I also wondered whats the point of the MEDIA_ROOT if all my files, images/videos and web design files, are just going into STATIC_ROOT? As far as I know, Django doesn't have a collectmedia command, so I don't really have anything collecting into the MEDIA_ROOT folder. I just have it out here, empty and all.
Am I missing something about this? Anyone understand Django's perspective on this? Whats your perspective? I am not sure if collectstatic should involve collecting media files as well, especially when I have a MEDIA_ROOT. I looked at the docs on static files and they really arent very helpful with regards to media files.
The Django documentation discourages having the static root and media root as the same directory (having them within the same parent directory is presumably alright). While not fleshed out in the docs specifically, the reason revolves around the inherent uncertainty when it comes to user-generated files. If both static and media files are served from similar directories, then a user, under the right circumstances, might be able to upload any file, which might subsequently be served. This of course creates major security vulnerabilities – this is the motivation for having two separate directories for these two classes of files.
collectstatic relies on the STATICFILES_DIRS array to generate a list of directories through which collectstatic should search for static files. If you are including media files in STATICFILES_DIRS, then, upon running collectstatic (as instigated either by you or by an automated service like Fabric), media and static files will live in the same output directory.
The documentation also has a section about Serving files uploaded by a user during development, which you can do simply by accessing the url at which the media files exist, as you would with any static file. You can simply append
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
to your URL configuration.
django.contrib.staticfiles.views.serve() however, should not be used for deployment. I've written more about this here. It is highly recommended that you serve your static files (and store and serve media files, as necessary) from a directory outside of your project's, in production.
Media files are files that users upload to your Django site.
Static files are files that you want to serve as part of the site (usually CSS, JavaScript and images).
As such I wouldn't expect collectstatic to do anything with media files.

Django 1.8 Not Serving Static Files in Development

I am using the exact settings as in https://docs.djangoproject.com/en/dev/howto/static-files/ and my app is not loading the files. I've spent way to long trying to solve something so trivial but it's just not working. I've looked at alternatives, but these didn't work either.
This is in my setting file:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
This is at the end my urls file:
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I did a collect static when I had the static file dirs so there should be the correct files in my static dir. I also created test files and nothing.
Heres what it will say in the debug view:
Request Method: GET
Request URL: http://localhost:8000/static/assets/stylesheets/style.css
Raised by: django.views.static.serve
Last time I wrote an app this worked fine, i'm not sure why is isn't now.
You haven't used the exact settings in the docs...Which is fine, but you need to at least do the following, if DEBUG = False:
Set up STATICFILES_DIRS. This should be the absolute path to the folder where your static assets live (before they're "collected").
Set up STATIC_ROOT. This is different than the above path, and is where all of your static assets will be collected when you run collectstatic.
Run the collectstatic command to collect (i.e. copy) your assets from your STATICFILES_DIRS to your STATIC_ROOT
Depending on your configuration, and if DEBUG = True, you don't even need to define STATIC_ROOT in development mode.

Confusion with serving static files with Django and fcgi

I thought I had this figured out, but I've read a few different things regarding this and I'm starting to get really confused.
The URL settings in my settings.py look like this:
MEDIA_ROOT = HOME + '/uploads'
MEDIA_URL = PUBLIC_HTML + 'media/'
STATIC_ROOT = HOME + '/static'
STATIC_URL = PUBLIC_HTML + 'static/'
ADMIN_MEDIA_PREFIX = PUBLIC_HTML + 'static/admin/'
Where HOME is /home/username/ and PUBLIC_HTML is like "http://www.mydomain.com/test/myproject/"
I seem to be getting my static files okay, but my media files don't show up. I don't have anything set up in my urls.py file, and I'm using shared hosting, so all I can do is use the .htaccess file for apache directives.
Obviously, I'd like to get my media files to show up, but my first question is why are the static files showing up? It doesn't make sense. I thought that apache had to intercept these requests before they were processed by Django, but they seem to be showing up fine without any kind of django.static.serve business in the urls. How is that possible? Am I doing this right (I'm sure I'm not), and how do I get the media files to show up? Why aren't they showing up when the static files aren't?
STATIC_ROOT must be served be apache on STATIC_URL, which you've done right apparently.
In the same fashion, MEDIA_ROOT must be served by apache on MEDIA_URL.
You could set up an alias, from MEDIA_URL to MEDIA_ROOT for example:
Alias /test/myproject/media/ /home/username/media/
It is preferable to avoid hard coding absolute paths, but first make sure that it works like this.

What is the difference between media and site_media folders?

What is the difference between the two folders? I created an example project in Pinax and sometimes they put stuff in media and other times site_media.
I believe MEDIA Is used for uploaded files while site_media is for static files.
https://docs.djangoproject.com/en/dev/topics/files/
Although the following quote is about staticfiles I think it covers the difference:
In previous versions of Django, it was common to place static assets
in MEDIA_ROOT along with user-uploaded files, and serve them both at
MEDIA_URL. Part of the purpose of introducing the staticfiles app is
to make it easier to keep static files separate from user-uploaded
files.
For this reason, you need to make your MEDIA_ROOT and MEDIA_URL
different from your STATIC_ROOT and STATIC_URL. You will need to
arrange for serving of files in MEDIA_ROOT yourself; staticfiles does
not deal with user-uploaded files at all. You can, however, use
django.views.static.serve() view for serving MEDIA_ROOT in
development; see Serving other directories.