Confusion with serving static files with Django and fcgi - django

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.

Related

uploaded files by users in website are not showing when debug is false

hey I've been working on my website for quite a while now and finally I was deploying it for production then I've faced with this problem that I can't server media when DEBUG = FALSE apparently Django doesn't serve media for you in production for some reasons,
IDK how to fix this and in my templates I've used this method to show different pictures that user have uploaded on website's database
<img src="{{site.logo.url}}" alt="logo" class="footer_logo">
now none of then show up in website
here are my media and static roots in setting.py
STATIC_URL = '/static/'
STATIC_ROOT = '../mywebsite/assets/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
# Additional locations of static files
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
I don't have a same problem with static files and I've almost tried all of the ways that others have recommended for same situation in other questions.
also I have no idea how to setup nginx but I have no problem with setting it up and learn about it if you guys can give me a guide an how to do it,
there was some tutorial on how to deploy django on Nginx with uWSGI but:
first idk what is uWSGI
and since I'm using cpanle idk if I can use uWSGI on it
and also can I do all of these without changing any part of my website ? (like my Views, Models, Template and stuff)
sorry if any of these questions sounds stupid
It's my first time deploying a website, I'm learning
Thank you :)

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.

Serving static admin files for Django 1.5 in a shared hosting environment

I'm trying to get a fresh installation of Django 1.5 running on an Apache-Server. The WebServer is situated on a shared hosting platform called uberspace.de
which means I have no access to the Apache configuration itself I can however write .htaccess files if that's any help at all. Django is deployed via fast-cgi which is working as expected.
Whats not working however is the access to static files on the server like the .css files and graphics for the Django administration interface.
As mentioned in the official docs I used the following command to copy the static Files into my ~/html/static directory.
manage.py collectstatic
And these are the values from my settings.py:
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = '/static/'
All I get is the infamous django 404 page when I try to access any of these Files.
I also followed the 'How to install and deploy Django' guide on my Webhosters Website to the letter. (sorry its only available in german I believe)
I already contacted the webhosters support but they don't know whats wrong.
All the solutions I've come up with so far suggest setting some sort of Alias in the Apache configuration. Which I can not do.
I'm thankful for any ideas you might have.
Try using a full address instead.
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = 'http://www.mysite.com/static/'
Edit: Perhaps you could ask your host to setup /static/ in your Apache config:
sudo nano /etc/apache2/sites-enabled/mysite.com and add:
Alias /static/ /home/bier/html/static/
I've had a situation before where I've had to upload /static/ files manually because of a highly restrictive host (permissions). Perhaps you need to download a copy of django to your desktop and then upload the static admin file set into your /static/ directory manually?
Lastly, have you added the static files to your urls?
url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root': '/home/bier/html/static'}),
I have more simpler solution. you need to create a directory named, let's say 'x' in "public_html" or similar location from which server serves the files by default.
Then upload all static files in directory x. (this can be done by running collectstatic locally and then upload all contents of directory STATIC_ROOT to x)
Then, change your STATIC_URL and STATIC_ROOT as follows:
STATIC_URL = '/x/'
STATIC_ROOT = os.path.join(BASE_DIR, '../public_html/x') # Path to folder

how to serve static/admin/ and not just static/css?

After doing the python manage.py collectstatic I found out that I can access everything in static/css but not in static/admin. To be very clear:
I go to: http://mysite.com/static/admin/css/login.css and fails
Manually move the folder static/admin to static/css
I go to: http://mysite.com/static/css/admin/css/login.css and works!
Notice the change from /static/admin/css/ to /static/css/admin/css/ in the URLS. This means that this doesn't solve the problem because the templates continue pointing to the first URL.
I know, serving static admin files has been asked 100 times in stackoverflow but I still cannot make it work (and I am not alone for the comments I have read). It seems nobody has mention this weird problem of accessing the static/css folder and static/anyother_folder.
Some extra details:
There is no errors in the collectstatic, there is no problems with the application css's. This is what I have in my settings.py):
MEDIA_ROOT = join(PROJECT_ROOT,'../media')
MEDIA_URL = '/media/'
STATIC_ROOT = join(PROJECT_ROOT,'../static')
STATIC_URL = '/static/'
I also have tried the deprecated ADMIN_MEDIA_PREFIX without any result.
ADMIN_MEDIA_PREFIX = '/static/admin/'
My nginx configuration is simple and clear.
location /static {
alias /home/the_home/where_the_static_is/static/;
}
location /media {
alias /home/the_home/where_the_media_is/media/;
}
I am using the last version of Django (1.4.2).
Puff. So, here it was. I had another folder in nginx redirecting the /static/admin/ to the Django installation in the virtual environment. In the organization, we use a virtual machine template that pre-configure our Django projects. So, revisiting the nginx configuration I found this:
location /static/admin {
alias /path/to/virtualenvs/my_virtualenv/path_to_the_static_admin;
}
Apparently this worked for Django 1.3 but the path changed at some point.