Django脆皮表单未加载CSS [英] Django crispy forms not loading CSS

查看:24
本文介绍了Django脆皮表单未加载CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SO 上看到了这个问题:Djangocrispy-forms 找不到 CSS,并遵循已接受答案中的所有建议,即:

  • 'crispy-forms' 列在 INSTALLED_APPS 下
  • 我没有运行生产服务器,所以我不确定是否应用了 collectstatic 选项(尽管我确实运行了它)

另外:我正在尝试使用引导程序模板包,所以我将 CRISPY_TEMPLATE_PACK = 'bootstrap' 添加到我的 settings.py 文件中.

当我从 https://gist.github.com/maraujop 加载示例创建的表单时/1838193,页面上没有CSS.有什么建议吗?谢谢!

我想我仍然缺少一些东西.我下载了 Bootstrap,将其解压缩到我的应用程序内的静态目录中,即:

my_app/静止的/css/js/图片/

我创建了一个 base.html:

{% 加载静态文件 %}<!DOCTYPE html><头><title>Bootstrap 101 模板</title><meta name="viewport" content="width=device-width, initial-scale=1.0"><!-- 引导程序--><link href="{{ STATIC_URL }}css/bootstrap.min.css" rel="stylesheet" media="screen"><身体><div id="内容">{% 块内容 %}{% 端块 %}

在我的模板中,我有:

{% 扩展 'base.html' %}{% 加载crispy_forms_tags %}{% 块内容 %}{% 酥脆形式 %}{% 结束块 %}

当我尝试加载我的页面时,表单加载(哇),但仍然没有 CSS.当我查看页面的来源时,可以看到以下内容:

但 Django 似乎无法找到该文件.

解决方案

您需要自己包含 css/js.所以... 下载引导程序,将文件放在您的静态文件目录中,包括引导程序的链接base.html 模板中的 css/js 文件,然后您将能够看到正确的格式.

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.

解决方案

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.

这篇关于Django脆皮表单未加载CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆