Django crispy forms not loading CSS

2019-08-29 04:14发布

I saw this question on SO: Django crispy-forms cannot find CSS, and followed all of the suggestions in the accepted answer, i.e.:

  • 'crispy-forms' is listed under INSTALLED_APPS
  • I'm not running a production server, so I'm not sure the collectstatic option applied (although I did run it)

Also: I am trying to use the bootstrap template pack, so I added CRISPY_TEMPLATE_PACK = 'bootstrap' into my settings.py file.

When I load the form created by the example from https://gist.github.com/maraujop/1838193, there is no CSS on the page. Any advice? Thanks!

Edit: I think I'm still missing something. I downloaded Bootstrap, unzipped it into a static dir inside my app, i.e.:

my_app/
    static/
        css/
        js/
        img/

I created a base.html:

{% load staticfiles %}
<!DOCTYPE html>
<html>
  <head>
    <title>Bootstrap 101 Template</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="{{ STATIC_URL }}css/bootstrap.min.css" rel="stylesheet" media="screen">
  </head>
  <body>
    <div id="content">
        {% block content %}{% endblock %}
    </div>
  </body>
</html>

In my template, I have:

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
   {% crispy form %}
{% endblock %}

When I try and load my page, the form loads (whew), but still no CSS. When I look at the source of the page, I can see the following:

<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">

but Django can't seem to find the file.

1条回答
我命由我不由天
2楼-- · 2019-08-29 05:02

You need to include the css/js yourself. So... Download bootstrap, put the files in your static files directory, include a link to the bootstrap css/js files in your base.html template, then you will be able to see the proper formatting.

查看更多
登录 后发表回答