I'm wondering where do you Django developers keep your media folder. I'm currently developing a project located at ~/apps/django-project. I am not using continuous delivery at the moment, but when I have to update the project, I ssh straight into the remote machine and pull the updated files using git (that's because it is placed in the project folder). The problem is that the media folder is always updating and I'll have to track them using git, which is not something I want to do. I don't think I'd like to .gitignore them too.
Would it be a good idea to structure the project like that?
Project: ~/apps/django-project
Media: ~/media
Statics: ~/static
If that's a good idea, could you give me a hint about how to set up my settings.py MEDIA_ROOT and STATIC_ROOT in order to accomplish this?
If that's not a good idea, I'd like to hear from you how would one structure the project and what other good principles should I take into account. (also, development / production tips are welcome)
Django3 with Python3.7
I create a folder public in my root directory and there I add the media and static directories
public/
media/
static/
I also add the specific paths to .gitignore, so they don't conflict between environments.
public/media
public/static
The good thing about this approach is that if you are using a webserver like nginx or uwsgi you can set the document root to public and serve any static file by default and resolve to django any other path that is not a file in public.
For example in nginx I do something like this.
server {
root /var/sites/myproject/public;
location #djangoapp {
proxy_redirect off;
proxy_pass http://localhost:8000;
}
location / {
try_files $uri #djangoapp;
}
}
This is very convenient because is easy to reason about public, everything that is in that folder will be served statically. For example I put there my robots.txt file and some others that I know they just need to be simple serve. It defaults nicely and really fast to django for any other request that is not an static file in public
public/
media/
attachs/
users/
static/
admin/
css/
js/
robots.txt
humans.txt
manifest.json
favicon.ico
I even once added support for .php files where I just put the files there and add the .php setup as an extension rule in nginx.
Related
So I'm trying to push my website into production, but I have encountered a problem while in the virtual environment, where my static files are not being found (404 errors).
In the settings.py file, I have STATIC_ROOT = os.path.join(BASE_DIR, 'static/').
In the file /etc/nginx/sites-available/django, I have modified the static files location, to look like this:
location /static {
alias /home/user/myproject/static;
}
In the directory /home/user/myproject, I have another directory called static, and inside are several directories holding the actual static files.
i.e. /home/user/myproject/static/shopApp/shop_app.css
or
i.e. /home/user/myproject/static/officeApp/office_app.css
What I am trying to see is if any of my configurations are set up badly. I am very new to django and web development, so I would appreciate any of your help!
EDIT: After doing some research I think I have the same problem as the guy from stackoverflow in this link: Fetching static files failed with 404 in nginx.
I think the second answer might solve my issue but I don't know where to apply the command chown www-data:www-data .
STATIC_ROOT only for prod, static folder for you app or project. PROD static != project static. For example you have 10 apps, like django admin. Without STATIC_ROOT and collectstatic command, you need point to each static folder in each app.
For example
location /static/admin {
alias /home/user/myproject/env../admin/static;
}
location /static/someapp {
alias /home/user/myproject/someapp/static;
}
But Django does a lot of work for you. You need to point to folder, which collects all static from all apps. For example STATIC_ROOT = os.path.join(BASE_DIR, 'static_remote/') and run collectstatic
location /static {
alias /home/user/myproject/static_root;
}
It turns out, that the problem was with my settings.py file.
Specifically, when your still using the django development server with your virtual environment (0.0.0.0:8000), you should have DEBUG set to True, so that it can serve your static files.
Remember! This is only if your using a django development server. For production, it should be set to False.
I got this information from this django article: https://docs.djangoproject.com/en/2.0/howto/static-files/
I am using Django and Apache.
I am serving the static files with Apache. It works well, the problem is, when a user go to the url http://urlOfMySite.com/static, he can see the whole directory and navigate in it.
I'm using : Alias /static /var/www/MySite/app/app/static in Apache VirtualHost to serve static files.
Is there any way to hide it from user? (make the static files accessible, but not the full directory browsable).
Thanks
Your problem is about the apache server itself, you need to disable indexing for that folder with "Options -Indexes" inside the "directory" clause to specify which folder you want to apply this command. More info:https://wiki.apache.org/httpd/DirectoryListings#Directory_Listings
Btw, this is odd, by default, when I deploy using Apache, I don't need this =/.
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
Django 1.4 has updates its default project layout, so my new project layout becomes
root
manage.py
/project
/settings.py
/app1
/static
/app2
/static
Now I'd like to use JavascriptMVC framework for my project, where is the best place to place it? It's huge(39M) just for the bare framework.
I don't want to have a framework inside each of my app. So, is there a way to host it in a central place and used by all my apps?
I personally put my project-wide static files in the top level directory (root/static/) and add it to STATICFILES_DIRS
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-dirs
It makes much more sense to me to keep it in the same level as apps. I only put project configuration related files in my root/project/ equivalent folder like settings.py, urls.py, local_dev flags / local settings, etc.
My django project in eclipse has this project structure:
main-project-folder/
src/
main-app/
app1/
app2/
settings.py
manage.py
urls.py
__init__.py
media/
templates/
Can i deploy the project with this structure? In other words, is right way to put src and other folders (media, tempaltes, etc.) in the root folder of my server (where my domain is linked)?
Like:
my-server-folder/
src/
media/
...
I imagine that in my-server-folder i should put the entry point of project, but in my project i haven't an entry point in main-project-folder, or does django automatically redirect to an entry point of src/main-app folder (i think that it doesn't because i don't find any options that say to django to do it)?
Sure. That's a fine directory structure.
Keep in mind your web server isn't going to know what to do with the Django project unless you tell it. If your web server is Apache (which it probably is if you don't know) look here for instructions to set it up to run the Django app:
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/
And here for WSGI:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
Django apps aren't like PHP where you just upload them to the web server and they work.