[Solved]-Bootstrap does not be set to Django app

9👍

I assume the project works fine in local, this problem occurs only when you use nginx. If that is the case probably the path you mentioned in the nginx config for location /static is wrong, try getting the absolute path to the static folder from the server and see.

5👍

I have no way to test my answer for obvious reason, but your STATICFILES_DIRS and STATIC_ROOT don’t match. If you change your STATICFILES_DIRS to [os.path.join(BASE_DIR,"/static/"),'https://bootflat.github.io'], it might work. I suggested so because it doesn’t make sense to me to os.path.join a local path with a public URL.

👤Mia

5👍

Try this,
In settings.py:

STATIC_DIR = os.path.join(BASE_DIR,"static")

Now at last, add these lines:

STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]

Now, put all your bootstrap files in a ‘static’ folder under your project folder.

At your HTML code,
You have to load the static files using:

{% load staticfiles %}

Now,you can use your static files using:

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

P.s. my ‘bootstrap.min.css’ is present under ‘static’ folder and then ‘assets’ and then ‘css’ folder.

It works perfectly for me.

0👍

First: where are you storing your static bootstrap files? I recommend storing them in the app’s directory where you are referencing them in a template. For example, if you have an app myapp I would put them in /myapp/static/myapp/. So if I’m storing bootstrap.css it would go in /myapp/static/myapp/bootstrap.css.

Next, in your template you need to reference the correct static folder. For the Bootstrap stylesheet, the href would look like href = "{% static 'myapp/bootstrap.css' }%"

Lastly, in your settings.py file add the line STATIC_ROOT = '/home/ubuntu/PythonServer/PythonServer/accounts/static' (based on what you wrote, just make sure that it’s pointing to the correct deployment folder where you’re keeping your live static files) and run collectstatic again.

👤Luke

0👍

First of all you do not need STATICFILES_DIRS since django will collect static files for you from each INSTALLED_APP specified in your settings.py. You need to add bootstrap4 to INSTALLED_APP:

INSTALLED_APPS = [
    ...
    'my_great_app',
    'bootstrap4',
]

You need to specify STATIC_ROOT and STATIC_URL as you do in your settings.py. You need to understand why you use each setting.

  • Django uses STATIC_ROOT in collect_static command which collects
    all static files from apps and directories in STATICFILES_DIRS puts
    in STATIC_ROOT. For example you created a new app which has it’s
    required static files in itself, you run ./manage.py collect_static
    to put your static files in STATIC_ROOT in which web server (nginx
    in your case) serves them. At your installation STATIC_ROOT should be
    showing to /home/ubuntu/PythonServer/PythonServer/accounts/static
    as seen in your nginx config.

  • STATIC_URL is used to create static file urls when you use
    static template tag. Since the files are served by nginx you need a
    specific path, host etc.

  • STATICFILES_DIRS is used to add static files which are not residing
    in apps folders. I do no recommend its use for your case.

On your template you need to use something like the following:

{% extends 'bootstrap4/bootstrap4.html' %}

{% load bootstrap4 %}
{# Display a form #}

<form action="/url/to/submit/" method="post" class="form">
    {% csrf_token %}
    {% bootstrap_form form %}
    {% buttons %}
        <button type="submit" class="btn btn-primary">Submit</button>
    {% endbuttons %}
</form>

Taken from django-bootstrap4 github repo.

By extending bootstrap4/bootstrap4.html you would get required css and js files in your html. With {% load bootstrap4 %} you can use bootstrap4 templatetags and filters.

0👍

Try by adding following code in your url.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += patterns('', (
    r'^static/(?P<path>.*)$',
    'django.views.static.serve',
    {'document_root': settings.STATIC_ROOT}
))

Add static root variable in settings.py as shown by Luke B

STATIC_ROOT = '/home/ubuntu/PythonServer/PythonServer/accounts/static'

Check here documentation https://docs.djangoproject.com/en/2.0/howto/static-files/

Leave a comment