I am developing an app in Django 1.6 and would like to know if the photos that I upload via the admin interface are "static files" in Django terminology.
I have this model:
from django.db import models
class ShowroomDetail(models.Model):
title = models.CharField(max_length=1000)
description = models.CharField(max_length=4000)
class ShowroomPhoto(models.Model):
showroom = models.ForeignKey(ShowroomDetail, related_name='photos')
photo = models.ImageField(upload_to='images/')
which I using as a basis of developing a page where there can be one or more images displayed along with the title and description. The images will only ever be uploaded by the admin interface and more photos for the page may be added at a later date.
So are these uploaded photo's "static files"?
No. An image uploaded through the website will be stored in the MEDIA_ROOT folder. It is considered then as a media file and not a static file. For your example, if MEDIA_ROOT = '/path/to/project/media/, then your photos will be stored in media/images/.
The difference is staticfiles will be collected for each application and are part of the code. Typically it is image for the design, CSS files, Javascript, etc. They are necessary to run the project.
Media files are files uploaded through the administration or by user through a special application to add content (avatar, gallery image, message attachment...). This content is independent of the code of the project and is considered as content.
Related: What is the difference between static files and media files in Django?
"static files" as a term of art refers to the static files, and the location you configure for it. Uploaded files could be found by static files if you upload them to a location which static files uses. Obviously, security issues abound.
Related
I am in the process of building a website with Django. The web app is for a local store in my city. The store sells gardening courses and workshops (the store is my mom's btw).
I want the admin of the page (the owner of the store) To be able to add new courses whenever she wants.
So I made this model for the courses:
class Curso(models.Model):
title = models.TextField()
photo = models.ImageField(upload_to='cursos/')
description = models.TextField()
price = models.IntegerField(null=False)
content = models.JSONField(blank=True)
clases = models.IntegerField(default=1)
duration = models.IntegerField()
isworkshop = models.BooleanField(default=False)
I included an ImageField because in the fronted there should be an image related to the course.
I added the path /cursos/ but I actually have no idea where the image is going. I saw people had a "media" folder.
But I also read the "media" folder was only for images uploaded by the user?
This is my static folder looks like:
static
|_app
|_js
|_css
|_sass
|_icons
|_images
|_homeslider
|_plants
|_pictures
Should the images uploaded from the admins app in that same folder?
Anyways I don't know where to store the images. And I don't have a hosting yet which leads me to my next question:
What should I do with the database?
I saw the majority of people asking these types of question had already bought a hosting so:
Should I buy a hosting even if the app is far from being ready just to start testing things out there and putting the courses objects there?
Are files stored differently in production?
Can I have my database locally and then upload it to the server?
What I don't want is to put a bunch of images and data on a sqlite database and then have to change all of it because that's not going to work for production
I'm new to web development and I am really lost when it comes to hosting, production and databases.
I would be very thankful for any help. I don't know what to do and I need someone more experienced to put me in the right direction.
Thank you in advanced!
So I've been working on a django project. Its a marketplace platform (two kinds of users - buyer & seller) and when a seller uploads files for a buyer, only those two users should have access to the uploaded files. I'm trying to figure how static files can be served with these permissions.
I'm not sure if this is relevant but I'm using nginx to serve the static files, so is there a way to add permissions logic in django to serve these static files?
A bit of Django terminology first: you're not talking about "static files" (those are the css, js, images and other assets that are part of your source) but about "media files" (user uploaded content).
Now for the technical answer: use nginx's X-Accel-Redirect feature. This delegates the permissions validation to your Django app, but still let nginx serve the file itself.
I'm trying to upload an entire folder (of images) inside the media_root in django.
I want something like im = models.ImageField(upload_to="images/", blank=True, null=True) but I do not want to upload many images one by one.
How can I do that? There is a simple way to use FileField or ImageField that upload directly the folder instead of a file/image?
I've done some digging around and it doesn't seem possible. According to this answer. You can use some JS to select all the files in the directory you want to upload and then create an upload field for each of the files.
I'm confused about static files and media files in django. I've seen elsewhere that people use it interchangeably.
When should I use media_root and when should I use static_root?
If I have site images should I put it in static? And if I have product images do I put it in media?
MEDIA_ROOT is the directory where file uploads are placed, as well as where generated files are usually stored. For example, one of my Django apps allows users to upload images. In one of the model classes, I use the ImageField type from sorl-thumbnail with upload_to='%Y-%m'. Whenever a user uploads an image, the file is stored in MEDIA_ROOT/%Y-%m/ (with %Y replaced with the current year and %m replaced with the current month number). Also, when sorl-thumbnail generates a thumbnail for an uploaded image, it places the thumbnail by default somewhere in MEDIA_ROOT/cache/.
STATIC_ROOT is used to configure the directory where static assets are placed. For example, site stylesheets, JavaScript files, and images used in the design of web pages are the types of files that go into STATIC_ROOT. If you have multiple installed apps, each app that uses static files can have its own static files directory. You use the collectstatic management function (invoked via python manage.py collectstatic) to copy all of the apps' static files into STATIC_ROOT.
Is it necessary to appoint MEDIA_ROOT, if indicated STATICFILES_DIRS? This issue arose when loading images - upload_to formed using MEDIA_ROOT and ignores STATICFILES_DIRS. If actually did not necessarily how to use upload_to with STATICFILES_DIRS?
Media and Static files might seem similar on first glance, but when you dig deeper you will find that Django draws a fine line between both. While Media usually refers to files uploaded by users, static files are created and bundled together with django apps.
The idea behind static files is, that upon release you can call
./manage.py collectstatic
and have all your static files from your apps (even 3rd party ones that live in egg files) collected into a given directory your HTTP server can serve directly (without any django/wsgi in the middle) for best performance.
The same holds true for Media files, but they are uploaded by users and not created by you or other app developers.
Hope that eases your confusion :-)
I recommend you take a look at the excellent documentation at the Django homepage:
Static files: https://docs.djangoproject.com/en/dev/howto/static-files/
Managing stored files: https://docs.djangoproject.com/en/1.3/topics/files/