I created a project in django and implemented django-hosts for subdomain of one of the apps, this work well on production. However, on deployment to digitalocean the static file couldn’t be located!
Pls, I want to ask the proper way of creating subdomain in django on production.
Thanks
I’d like a little more detail about what’s happening here. Django is typically unaware of the domain name under which it’s being accessed - and if you’ve got your web server configured correctly, it doesn’t even see requests for static files.
So I think to diagnose this, we’re going to need to see an example of the URL that is being rendered for a static file, information on how you’re running the application itself (uwsgi? gunicorn?) and how it’s configured, what web server you’re running (nginx? apache?) and how it’s configured for static files, and your static file-related settings in your settings file.
An example of URL for static file is executives.kindomleadsafrica.org/static/main/CSS/styles.CSS
Hosts.py looks like
host_patterns = patterns(' ',
....
host(r'executives.kingdomleadsafrica.org', 'executives.urls', name='executives')
......
)
I run the project on gunicorn and nginx
Settings.py for static looks like
STATIC_URL = '/static/'
import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Nginx configuration looks like
server {
listen 80;
server_name mydomain.org www.my do main.org;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sammamy/myprojectdir;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
You can check the subdomain by going to executives.kindomleadsafrica.org
Thanks for your response
The error I saw in the console is refused to style from because its MIME type (‘text/html’) is not a supported stylesheet mime type, and strict mime checking enabled.
The URL is similar to the one shown above
That’s the error that occurs when a stylesheet can’t be loaded. (Django returns a 404, which is an html page and not a css stylesheet.)
You need to verify that the file styles.CSS is in the directory /home/sammamy/myprojectdir/static/main/CSS/
You also need to verify that nginx has the necessary permissions into that directory tree.
Also, you probably want to add executives.kindomleadsafrica.org
to your server_name
directive.
/home/sammamy/myprojectdir/static/main/CSS is indeed correct.
How do I know? kingdomleadsafrica.org and other pages with the subdomain are inheriting from the same static files. Other pages are working perfectly but for the subdomain.
Kindly take a look at the home page and other pages.
Thanks a bunch
I’m not saying that the directory is wrong - I’m asking you to verify that the file styles.CSS
is in that directory, and that it has permissions such that nginx can access it.
You may also want to check your nginx logs to see what is showing up there.
These aren’t the types of issues that can be diagnosed by looking at the site - you need to examine how it’s deployed and what nginx is trying to retrieve.
Yes, styles.CSS is located there and nginx has permission to access it
An additional note - I can’t access http://executives.kindomleadsafrica.org/, I get a dns resolution error.
Also keep in mind that file and directory names are case-sensitive. The file styles.css is a different file than styles.CSS.
Not been able to access it may be because of certbot certificate which I will remove until I’m able to find solution or know how to create subdomain.
I meant styles.css
Thanks for your responses.
I’d like if you can let me know the correct way of creating subdomain in django apart from django-hosts.
Maybe an implementation at production
This issue has nothing to do with Django or django-hosts. Static files are not handled by Django in a production environment.
This is strictly an issue of nginx and your static file deployment.
1 Like
Ehn, I’d looked into it but I wonder why other parts of the sites are able to inherit the static files but for the subdomain
Pls, kindly check the subdomain now, it’s working but no static files
Yep, just checked it out.
You’re trying to access, for example, https://executives.kingdomleadsafrica.org/static/main/css/styles.css
but nginx is returning a 404.
However, https://kingdomleadsafrica.org/static/main/css/styles.css
returns a valid css file.
You have an nginx configuration issue. This is not a Django issue.