Admin CSS and Icons missing

The admin styling and all static icons are not working on recently deployed site. I’ve looked at other posts on this site and tried some of the suggestions with no success.

I believe that my static files are being served correctly but this does not appear to be true. When I ran python manage.py collectstatic “all” the files were copied to my STATIC_ROOT = os.path.join(BASE_DIR, 'static/') and STATIC_URL = '/static/'.

I had an error when I ran python manage.py check --deploy that read:
?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace. I found that path('admin/', admin.site.urls), was defined in the main project folder and in a customized user app I created. Commenting out the pattern in the users app fixed that error. I now have no errors. I’ve cleared the cache over and over. I’ve restarted the python app (using Python under DirectAdmin on a CloudLinux ISP). I’ve checked the html for correct tag spacing. I’m really not sure what to do next.

admin base.html:

{% extends 'admin/base.html' %}{% load static %}

{% block branding %}

<h1 id='head'> 
<img src= "{% static 'mealapp/Thrashers_Icon1.png' %}" width= "50" height="50" />
Admin
</h1>

{% endblock %} 

{% block extrastyle %}
<link rel="stylesheet" href="{% static 'css/adminstyle.css' %}" />
{% endblock %}

Here’s the html for one of the icons that not showing;

<img src="{% static 'mealapp/Thrashers_Icon1.png' %}" alt="" width="75" height="75"

Installed packages:

asgiref==3.2.10
Django==3.1
django-crispy-forms==1.12.0
numpy==1.21.1
pandas==1.3.1
Pillow==8.3.1
python-dateutil==2.8.2
pytz==2021.1
reportlab==3.6.1
six==1.16.0
sqlparse==0.4.1

Any ideas on where to look would be appreciated.

How are you deploying your site? Under apache or nginx?
Do you have the right “static” url and directory configured for your web server?
Have you visually verified that the static files are in the desired directory?
Does your web server process have permissions to those files?
Are you getting any error messages in your web server logs?
(It might be worth turning on more verbose logging to see what error messages may be generated.)

(Note: Under a common linux-based deployment environment, you don’t really want your static files collected anywhere within your project directory structure. I always recommend that your collection point be somewhere where the file server would “naturally” have access to it - otherwise you may encounter permissions issues.)

Also see Deploying static files for other information.

How are you deploying your site? Under apache or nginx?

My ISP converted from cPanel to DirectAdmin during the deployment. The db is found under the MySQL Management. All the tables are working correctly.

Do you have the right “static” url and directory configured for your web server?

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'

Have you visually verified that the static files are in the desired directory?

Yes, I did verify that the 152 static files were in fact copied to the path specified. I also noticed that an admin directory was created with /css /fonts /img /js folders. I was not expecting that. I did not create the files in those folders. They appear to be Django files.

admin/css/

image

Does your web server process have permissions to those files?

I don’t know. How would I check this?

Are you getting any error messages in your web server logs?
(It might be worth turning on more verbose logging to see what error messages may be generated.)

How do I look at server logs?

(Note: Under a common linux-based deployment environment, you don’t really want your static files collected anywhere within your project directory structure. I always recommend that your collection point be somewhere where the file server would “naturally” have access to it - otherwise you may encounter permissions issues.)

Do you think this could be the issue if the static folder is in the project “root”?

image

Also see Deploying static files for other information.

That is correct. Collectstatic mimics the apps’ directory structure within the target directory

Possibly. I see where the directory you highlighted (static) is world-readable. You could drill down into all the subdirectories and files within it to verify that everything in there is at least “rwxr-xr-x”.

Those are administrative tools. The web server itself is likely one of apache, nginx, gunicorn, uwsgi or lightspeed - each of which have different configuration files.

Check your server’s configuration to see what account the server is running as. Also check to see if it identifies some directory as a base directory for static files. (Note, this is the server’s base directory, not Django’s.) It’s typically going to be something like /var/www/html.

Most of the time, the server logs are in /var/logs

Note: All of this assumes you have command-line (shell) access to the server. If you do not, then you need to find whatever documentation is provided by your hosting provider to dig up the information you need - at that point it’s no longer a Django-related issue but a hosting provider issue.

To be crystal clear, the static folder is in the parent folder of the project. Not in the project folder.
settings.py entries;

BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'

Do you have any recommendations on another location? There does not appear to be any issues with the db. The only issue I’m having with site is with the styling in the admin section and all icons in the main site not being found.
From what I see the only problem is with Django and static.

The database has nothing to do with this. In the vast majority of deployment environments neither Django nor the database are involved with serving static files. They are served directly by the web server.

From my earlier response:

I can’t be any more specific than that, since I don’t know what web server you are using or how it is configured.

Actually, this is not even a Django issue - this is a deployment / web server configuration issue. Again, the link referenced above for Deploying static files has more information about this.

Django is not intended to serve static files. Or perhaps to be more accurate, it would be the choice of last resort. There are so many better ways to serve static files than through Django that it’s almost never the best available solution. (Acknowledging some extreme edge cases that are the exception to this general principle.)

It may also be worth your time to review Deploying Django and the Deployment checklist.

As it turns out WSGI is provided through mod_passenger. It executes the file passenger_wsgi.py and the rest of my app is imported from there.

Cool, we’re getting closer. The module mod_passenger is to my knowledge an apache module.

That means you only need to find out where apache is configured to serve static files from - or what you need to change to specify where those files are to be found.

Generally, you’re going to find that information either in /etc/apache2/apache2.conf or within the /etc/apache2/sites-available (or sites-enabled if it’s not symlinked) directory. You’re looking for the DocumentRoot directive that applies to your site.

It’s also possible that they’re using uwsgi to serve the static files, in which case you’re looking for a static-map (or static-map2), check-static or route directives within the uwsgi.ini file being used.

Thanks Ken, I’ve put in another email to my ISP to find out how to access the Apache configuration. There is nothing in the DirectAdmin dashboard. Apache Handlers is one of the advanced features but there are no handlers configured. My ISP moved to DirectAdmin at the same time I was deploying my site. I have root access. There are two /etc folders that I can see. One is at the very root of my account and the other one is at the root where my project folder lives among other folders, two levels up. Neither of the /etc folders contain an apache2 folder.

Either that, or if they could just tell you what “DocumentRoot” is for your instance. (Once you have that, that is what you set STATIC_ROOT to - that would be the target location for your static files - assuming you only have one application running on that instance. You have to do a little more if you’re running multiple applications on that host.)

A little frustrating but I did get a fix and created another problem.

I was asked to put my static folder into /domains/mysite.com/public_html. So I did, and then the icon on the main page that was missing previously, now appears. Problem one solved.

I was logged in when I made the change and restarted Django. With my ISP this is done through DirectAdmin and restarting the Python web app. The styling or most of it now appears on the admin page.

But then I started doing some testing. I logged out and tried to log in again. Logging in brought up a 500 error. I think this may be due to the redirect I have set in my code after a user is authenticated. I’m thinking that there may be a setting that denies a redirect or perhaps a file permission. Do you think that could be a possibility? Any thought on other reasons for the 500 error?

Do you know if I can restart Django with a terminal command? I can log in and run my Virtual Environment typing this command at the prompt:

source /home/virtualenv/MyApp/3.8/bin/activate && cd /home/MyApp

You don’t run your app directly. The web server process is responsible for doing that.
As you pointed out:

So that process isn’t under your control.

A 500 error can be caused by a large number of different errors. If you can’t find / get access to the log files, you could either turn DEBUG=True for a short period of time to find the problem, or make sure your email environment is set up correctly for this and have Django email the errors to you. But by itself, a 500 doesn’t really tell you anything.