Django admin doesn't show CSS after deploiement DRF - css

I have build a web application using Django Rest Framework and React, I am using IIS for deployment. It works just fine but I have a problem when trying to deploy Django Admin. The style doesn't show. It shows this:
I have tried so many methods to add style to DRF project.
I used collectstatic and added it as application to IIS Manager
I activated mimetype in settings.py to accept .css
I tried to link css files in the to contrib/static with the /static url and added that into urls.py.
None of the above methods were able to solve my problem and I have stuck with this bug for days.
Could you help me to figure out this problem.
settings.py config

Your STATIC_ROOT seems to be incorrect. In fact, it will be the place where all statics will be collect by the collectstatic command.
So it has to refer to a path on your server.
You have two possibilities :
Build a relative path to a folder called "collected_static" or whatever name you want :
STATIC_ROOT = BASE_URL / 'collected_static'
Build an absolute path to a folder in your server like :
STATIC_ROOT = '/var/www/my_proj/statics
In development mode, you don't need to collect static, because django knows how to collect dynamically.
In production mode, you need to collect static, because it will provide and group all your statics inside the specified path.
After running python manage.py collectstatic, what is the response ? And can you see your expected statics in the expected folder ?

Related

Django project does not work after being transferred from Windows to Mac

I'm using Django 3.1.7 on a MacBook Pro after transferring the Django project off of a Windows 10 machine. However, upon re-setting up the database and superuser and running the command pipenv sync to install all necessary modules, the server does run; but it doesn't serve any of my URLs or the CSS styles for the Admin page. I can reach the admin page at '/admin' but no other page that I've set up will display. All it gives me is my custom 404 page whenever I try to serve a page. Is there any way to fix this? Is something wrong? Where are the Admin CSS files?
python manage.py runserver --insecure
Use the --insecure option to force serving of static files with the staticfiles app even if the DEBUG setting is False.
djangoproject
I think we'd need more information... maybe the files itself.
There could be tons of things that could go wrong. Is your settings.py still pointing to the correct file paths? In views.py, did you make sure you are setting the html files in the same path? Are the html files in the right folder? Are all the Django and module versions the same? There's too much to name.
I have the same problem.
By what you are describing it happens because your file path uses either / or
One machine uses forward slash and the other backwards slash
I dont know to so use a method that work on both

Django 1.9 admin not showing CSS style

I am new to django and currently I am making the tutorial. It goes well but every time at some point my admin site loses ist CSS styling and starts looking like this:
This problem is apparently not new, as I found few posts here (e.g., this one or this one) on the topic. Following the answers I made the next steps:
Running python manage.py collectstatic
Modifying settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Adding the following lines to manage.py:
import mimetypes
mimetypes.init()
mimetypes.types_map['.css'] = 'text/css'
Going sure that 'django.contrib.staticfiles' in settings.py is uncommented.
Nevertheless, nothing worked and my admin site still does not have CSS styling.
Note that in my case the problem is not consistent. Whenever I start a new project, the admin page is fine. The problem appears only after certain (and each time different) step throughout the tutorial. For example, last time it was after I modified the DATABASES entry in settings.py (though I went through this step smoothly one attempt before; changing the entry to its original value did not help to restore the admin page styling).
I am using django 1.9.6 with python 3.4.3 under Ubuntu 14.04.
EDIT:
I am running server with #DEBUG = True and ALLOWED_HOSTS = ['*']
Django does not serve any static or media files when DEBUG = False.
Changing the variable to True and the static files for the admin site will appear. Otherwise, you will have to use a web server program to server the admin files as well as any other static/media files included in your project.

changing ROOT_URL for meteor app

I am trying to get my app to run behind an NGINX reverse proxy and had some minor success.
the path is http://dev.sertal.ch/myApp and the application is accessible.
The issue I am still facing is that the images in the public folder are not accessible without hard coding myApp at the start of the URL. This is especially an issue for URLs inside the CSS.
You would want to set the ROOT_URL environment variable when you start your meteor app. If you are using meteor to start from the command line in your app's directory it would be like this:
ROOT_URL=http://dev.sertal.ch/myApp meteor
Meteor has a ROOT_URL property which you must explicitly set for your bundled applications.
It is in the form of Meteor.absoluteUrl([path], [options]) and the path argument is exactly what you're looking for, excerpt from the docs:
A path to append to the root URL. Do not include a leading "/".
Check here for details on options http://docs.meteor.com/#/full/meteor_absoluteurl

My haml view is unable to find the css file in my public/css folder when deployed, but manages to find it on localhost

My application is running on Sinatra and deployed on my Apache web server using Passenger. My directory structure is as follows:
approot
` public
` css
- bootstrap.css
` uploads
- empty.txt
` tmp
- restart.txt
` views
- success.haml
- upload.haml
- config.ru
- myapp.rb
Inside upload.haml
%link(rel="stylesheet" href="css/bootstrap.css")
When I run this application on localhost:4567, the CSS loads just fine. However, when I deploy it on my web server, the CSS doesn't load.
On my web server, the application is accessed using: rubyapps.mydomain.com/appname
And if I type: rubyapps.mydomain.com/appname/css/bootstrap.css, I am able to see the contents of my CSS file just fine.
Totally confused, and not getting how Sinatra handles this situation, looking for a little help.
You might be running into the need to use Sinatra's URL helper.
For generating URLs you should use the url helper method, for instance, in Haml:
%a{:href => url('/foo')} foo
It takes reverse proxies and Rack routers into account, if present.
This method is also aliased to to (see below for an example).
Maybe this?
%link(rel="stylesheet" href="../public/css/bootstrap.css")
Or...
%link(rel="stylesheet" href="/css/bootstrap.css")

Django external css file problem

For a couple of days now I have been trying to setup my django project to run my html-template with an external css-file. So far, no succes....
I have installed staticfiles ( Im using django 1.2.4.) and put the 'staticfiles' in INSTALLED_APPS within settings.py and added the following code:
STATIC_ROOT=os.path.join(os.path.abspath(os.path.dirname(file)), "static")
STATIC_URL='/static/'
My css-file is located under /static/css/stylesheet.css
My html-template has the link
link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/stylesheet"
After running the server, the page loads just fine. However django cant find my stylesheet...
What am I doing wrong here?
The static root and url doesn't actually host the files. The static serve option (in the urls.py) mentioned previously is a good option for development and learning, but if you move to a deployment server you should use the static hosting provided by your webserver.
The way the static folders is intended to work is that you add the path locations for each app, project, etc to the static directories setting in settings.py. Then, when you run the command "django-admin.py collectstatic" django pulls all of your directories into your static root. After the first time you run collectstatic, only files that have changed will be copied again. This consolidates multiple static directories into one common place.
Static files documentation
You need to pass the RequestContext to the view, so it will run through the staticfiles' CONTEXT_PROCESSORS (which includes the STATIC_URL variable).
from django.template.context import RequestContext
context = {'my_other_context': 1}
render_to_response('your_template.html',
context_instance=RequestContext(request, context))
I would recommend you to just use a django.views.static.serve instance like this in the url.py file:
(r'^(?P<path>.*)$', 'django.views.static.serve',{'document_root': '/path/to/css/'}),

Resources