简体   繁体   中英

django css works on chrome but not firefox

I'm developing django website and am using bootstrap, I can see the styles on chrome browser but not firefox.

firefox inspection shows 404 error for the bootstrap and chrome doesn't. Thankful for any idea.

Code :-


#settings.py 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))

STATIC_URL = '/static/'
STATIC_ROOT = '%s/coffestatic/' % (BASE_DIR)

STATICFILES_DIRS = ['%s/website-static-default/'% (BASE_DIR),
                     ("bootstrap", '%s/bootstrap' % (BASE_DIR)),]

HTTP file.html

<head>
{% load static %}

<link href="{% static'bootstrap/css/bootstrap.css'%}"rel="stylesheet">
</head>

Files structure

BASEDIR

|--ProjectFolder

|------Apps

|--BootstrapDir

|------css

Work flow

  • Define static files
  • python manage.py collectstatic
  • Define styles in html
  • run project

Thanks

I'm not sure if your settings.py files are correct as they are different from how I would set things up. That being said, I'm no expert and you may well be running a different version of django to me. What I did notice though is you don't have a file type on your link file. Maybe Chrome is fine without this but maybe Firefox is having a hissy fit without it. Try changing:

<link href="{% static'bootstrap/css/bootstrap.css'%}"rel="stylesheet">

to

<link href="{% static'bootstrap/css/bootstrap.css'%}"rel="stylesheet" type="text/css">

Just a guess....

Using bootstrap starter file in your html file will help; as I have done the same and it helped. Add these files at the end in body section:

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRS

and add these files at the starting in head section:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

I'm a beginner at django so not sure about it but it worked for me.

In project folder create a directory with any name. Let's name __shared__ and put the css and jss folders inside the directory

and in settings.py at bottom put this code

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "__shared__"),
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM