Subdomain created with django-host not styled on prodluction

156 views Asked by At

I recently deployed a django project to digitalocean. This project has some django apps and implemented django-hosts for subdomain configuration. While this worked well locally, static files could not be found on navigating to the subdomain after deploy to digitalocean.

All the other parts of the websites are styled properly.

Question:

  1. Is there anything I can do to correct this anomaly?
  2. Is there any other way to do this without having to use django-hosts.

Here is the website: kingdomleadsafrica.org

The subdomain is: executives.kingdomleadsafrica.org

Thanks

2

There are 2 answers

0
Bikesh Sapkota On

Check Static Files Configuration: Verify that your static files are correctly configured in your Django settings. Ensure that you have set the STATIC_URL and STATIC_ROOT correctly.

In your settings.py, you should have something like this:

STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL is the URL from which static files will be served in your templates, and STATIC_ROOT is the directory where collectstatic will copy the static files when you run it.

Run collectstatic: In production, you need to run the collectstatic management command to collect all static files from your apps and place them in the directory specified by STATIC_ROOT. Make sure you run this command whenever you deploy a new version of your application.

Run the following command:

python manage.py collectstatic Check File Permissions: Ensure that the web server running your Django application has the necessary permissions to access the static files. Verify that the user running the server has read permissions on the static files and directories.

Check Static Files Are Present: After running collectstatic, check if the static files have been copied to the directory specified by STATIC_ROOT. Make sure the static files are present and accessible.

Test Static URL: Check if you can access your static files directly by visiting their URLs in the browser. For example, if your STATIC_URL is set to /static/, try accessing a static file like https://gwtc.tech/static/blog_app/style.css https://gwtc.tech/static/blog_app/revolution/css/layers.css

Check for Errors in Browser Console: Open your browser's developer console and check for any errors related to loading static files. Look for 404 errors or any other issues.

Check Server Logs: Check the server logs for any error messages related to static files. In many production environments, server logs are located in the /var/log directory. Look for any errors related to serving static files.

Check Web Server Configuration: If you are using a web server like Nginx or Apache to serve your Django application, ensure that the web server configuration is set up to correctly handle static files. Check for any misconfigurations in the web server's configuration files.

By following these steps, you should be able to identify and debug the issues with static files not working in your Django application when deployed to production. Common issues are related to misconfiguration, file permissions, or web server setup. Once you identify the problem, you can take appropriate steps to fix it and get your static files working as expected.

0
Ojo Philip Odeniyi On

Making a subdomain in django at production is simple and straight forward.

N.B: you don't need any plug in like django-hosts or django-subdomain to make this happen.

What I did with my project is splitting it into two different projects calling them different names.

I put both of the projects in the same projectdir as recommended when deploying single project to digitalocean using nginx and gunicorn.

I created gunicorn.sock and gunicorn.service for each project changing gunicorn to the name of the project e.g if my projects name is blog and forum, I will have blog.sock and blog.service for blog and created corresponding name for the other project.

The same thing will be done for nginx too. You place the domain that you want in the server part of the nginx.

Make sure that you properly fill the directory well especially the gunicorn.service(in this case, blog.service and the other).

Also, if you want to run any command relating to gunicorn, you will the name of your service file.

Remember to include your subdomain in in your settings.py and also create it in your digitalocean as A record

Good luck.