What is the difference between media and site_media folders? - django

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.

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.

What is the use of STATIC_URL

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.

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.

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.

Django Static Files Development

This seems to be a source of much confusion judging by the amount of similar titles on the subject, however trying everything I could find on static files with the django development server I've almost given up hope!
So my static files are served from C:/Users/Dan/seminarWebsite/static/, in which I have sub folders for images, css etc.
SETTINGS:
STATIC_ROOT = 'C:/Users/Dan/seminarWebsite/static/'
STATIC_URL = '/static/'
The static files app is also active.
URLS:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
TEMPLATE:
"{{ STATIC_URL }}images/vision.jpeg"
However only a broken link appears and at this address: http://127.0.0.1:8000/homepage/images/vision.jpeg and I don't think it should be at that address (homepage is the url name of the page the static image file is being called to).
Based on what you've posted so far, it looks like you're following the docs for django.contrib.staticfiles. I agree that the docs can be difficult to follow especially if one is new to django.
I believe the confusion stems from the fact that django.contrib.staticfiles has two modes of operation:
During the development phase where the development server is used, it dynamically searches for static files in predefined directories and make it available on STATIC_URL
For deployment, it assists in collating static files to a single directory (defined using STATIC_ROOT) so that the static files can be hosted using a webserver suited for static files. This collation is done using python ./manage.py collectstatic.
Here's a quick summary of how to get up and running. I haven't had a chance to try it out so there may be mistakes. Hopefully this will help you get started and at the very least help you understand the docs. When in doubt, do refer to the docs.
Hosting static files on the development server
Make sure you have 'django.contrib.staticfiles' in INSTALLED_APPS
Specify STATIC_URL. This will be the path where your static files will be hosted on.
STATIC_URL = '/static/'
Make sure your files are in the correct directories. By default, staticfiles will look for files within the static/ directory of each installed app, as well as in directories defined in STATICFILES_DIRS. (This behaviour depends on backends listed in STATICFILES_FINDERS).
In your case, you'd probably want to specify your directory in STATICFILES_DIRS:
STATICFILES_DIRS = (
'C:/Users/Dan/seminarWebsite/static/',
)
Make the view accessible by adding the following to the end of urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Make sure you have DEBUG = True in settings.py.
That's it.
If you run your dev server (./manage.py runserver), you should be able to access your file via http://localhost:8000/static/images/vision.jpeg (which serves C:/Users/Dan/seminarWebsite/static/images/vision/jpeg).
Linking to static files in your templates
There are two ways to get a correct link for your static files - using the staticfiles template tag, and making STATIC_URL accessible to your templates. Since you've attempted the latter, we'll stick to that.
Make sure you have 'django.core.context_processors.static' in TEMPLATE_CONTEXT_PROCESSORS. If you haven't redefined TEMPLATE_CONTEXT_PROCESSORS then there is nothing to do since it should be there by default.
Make sure you use RequestContext when rendering your template. Example:
from django.template import RequestContext
# ...
def some_view(request):
# ...
return render_to_response('my_template.html', {
"foo" : "bar", # other context
}, context_instance = RequestContext(request))
You should now be able to use the following in your my_template.html:
<a href="{{ STATIC_URL }}images/vision.jpeg" />
Hosting static files on production server.
If all the static files you need to serve are store in that one directory (C:/Users/Dan/seminarWebsite/static), then you're almost there. Simple configure your webserver to host that directory on /static/ (or whatever you set STATIC_URL to) and you're good to go.
If you have files scattered in different directories and/or app specific static files, then you'll need to collate them.
Set STATIC_ROOT to the directory where you want to store the collated files.
Run ./manage.py collectstatic to do the collation.
Configure your webserver to host the that directory (STATIC_ROOT) on /static/ (or whatever you set STATIC_URL to).