1
I need to know how to serve these files that usually go to the root of the project. I’m making a route for each of them in the.py urls file but it doesn’t seem the right way. What’s the right way?
1
I need to know how to serve these files that usually go to the root of the project. I’m making a route for each of them in the.py urls file but it doesn’t seem the right way. What’s the right way?
2
Generally the most correct is to delegate this task to Nginx or Apache, which in most cases will already be serving the static files of the project.
If you are using Nginx, follow an example:
server {
server_name example.com;
access_log /opt/example.com/logs/nginx_access.log combined;
error_log /opt/example.com/logs/nginx_error.log;
location /static/ {
alias /opt/example.com/static/;
}
location /media/ {
alias /opt/example.com/media/;
}
location = /favicon.ico {
access_log off;
log_not_found off;
rewrite (.*) /static/img/favicon.ico;
}
# restante do arquivo de configuração...
}
I usually use this setting to favicon.ico
, robots.txt
and sometimes to the sitemap.xml
when this is static. If your application is one focused on SEO content is important, it might be worth considering the Sitemaps framework, which is part of the Django apps.
But back to your question. The example above would be for Nginx. If you are using Apache, follow an example taken from official documentation:
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
<Directory /path/to/mysite.com/media>
Require all granted
</Directory>
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Browser other questions tagged python-3.x django
You are not signed in. Login or sign up in order to post.
'These files' do you mean static files ? - Serve in what context? Location or production ?
– Otávio Reis Perkles
@Otávioreisperkles believe that yes, they are static. It would be in production. Every file they send I have to create a route for them and throw them in the templates folder and I don’t think that’s right. In theory it would just put in the root and would be good.
– dsantoro
Are you talking about having a separate folder just for static files? Have you seen this page in the documentation?
– Jefferson Carvalho