I need to know how to serve these files that usually go to the root of the project. I'm making a route to each of them in the urls.py file but it does not seem to me the correct way. What is the right way?
I need to know how to serve these files that usually go to the root of the project. I'm making a route to each of them in the urls.py file but it does not seem to me the correct way. What is the right way?
Generally the most correct is to delegate this task to Nginx or Apache, which in most cases will already be serving the project's static files.
If you are using Nginx, here's 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 for favicon.ico
, robots.txt
and sometimes sitemap.xml
when it is static. If your application is a focused SEO content is important, perhaps it would be worth considering the sitemaps framework , which is part of Django apps.
But back to your question. The above example would be for Nginx. If you're using Apache, here's an example 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>