django脆皮形式按钮没有显示

时间:2013-11-21 17:41:42

标签: python django twitter-bootstrap django-crispy-forms

我确信我非常密集,但花了几天时间试图解决我做错了什么。

我有

  • 安装了django-crispy-forms(使用pip)
  • 将“CRISPY_TEMPLATE_PACK”添加到我的设置
  • 将'crispy_forms'添加到我的应用程序
  • 从下载的tar文件中完成'collectstatic'(因为模板和CSS不包含在点子中)
  • 将“{%crispy_forms_tags%}”添加到我的表单html文件
  • 从crispy_forms.helper和crispy_forms.layout导入到我的forms.py

我正在使用Bootstrap 3,我直接编码而不是使用bootstrap-toolkit

Bootstrap在我的所有页面上渲染都很好,表单字段也是如此,但按钮不会出现。

我的HTML如下:

{% extends 'base.html' %}
{% load crispy_forms_tags %}

<html>

<title>Add Guest</title>


<div class='container'>
    {% block col1 %}
    <div class='row'>
        <div class="span3">
        {% endblock %} 
        {% block col2 %}
        <div class="span9">
            <ul>
                {% crispy form %}
            </ul>
        </div><!--/span9-->
        {% endblock %}
    </div><!--row-->
</div><!--/container-->

</html>

如果我将crispy标签更改为{%crispy MyForm MyForm.helper%},我会得到'VariableDoesNotExist at'以及连续的许多(真实)语言。如上所示,表单字段显示但不显示按钮。

我的forms.py看起来像这样:

from django.forms import ModelForm
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit
from .models import Author

class NewAuthorForm(forms.ModelForm):
    class Meta:
        model = Author
        # If you pass FormHelper constructor a form instance
        # It builds a default layout with all its fields
        helper = FormHelper()
        helper.form_method = 'post'
        helper.add_input(Submit('save', 'save', css_class = 'btn-primary'))

我知道在进口方面存在一些冗余,但这不是问题所在。

最后,我的base.html是:

<!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">

    <!-- note that there’s also blue.uni-form.css and dark.uni-form.css available if you want to try chan-->
    <link rel="stylesheet" href="{{ STATIC_URL }}crispy_forms/static/uni_form/uni-form.css" type="text/css" />
    <link rel="stylesheet" href="{{ STATIC_URL }}crispy_forms/static/uni_form/default.uni-form.css" type="text/css" />
    <!-- uni-form JS library, optional -->
    <script src="{{ STATIC_URL }}crispy_forms/static/uni_form/uni-form.jquery.js" type="text/javascript"></script>


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
        <div class='container'>
            {% block col1 %}
            <div class='row'>
                <div class="span3">
                {% endblock %} 
                {% block col2 %}
                <div class="span9">
                </div><!--/span9-->
                {% endblock %}
            </div><!--row-->
        </div><!--/container-->

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://code.jquery.com/jquery.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="{{ STATIC_URL }}js/bootstrap.min.js"></script>
  </body>
</html>

我将crispy表单模板放在我的templates文件夹中,该文件夹与manage.py处于同一级别。正如我所说的,所有的bootstrap渲染都很好。

我很难过。任何想法都感激不尽

2 个答案:

答案 0 :(得分:2)

我完全不知道为什么,但以某种方式删除

from __future__ import absolute_import

从视图中为我解决了问题,现在我的按钮出现了。

P.S发现这个问题,谷歌搜索解决方案,并决定注册并希望帮助下一个解决这个问题:)。

答案 1 :(得分:0)

这是一篇很老的帖子,但如果我在搜索时就这么做了,那么其他人可能会这样做。

我使用的是Django 1.11.9和django-crispy-forms 1.7.0。您已经走上了正确的轨道,实际上您想要添加“提交”#39;该类的 init 按钮,而不是Meta。

见下文:

from django.forms import ModelForm
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit
from .models import Author

class NewAuthorForm(forms.ModelForm):
    class Meta:
        model = Author

    def __init__(self, *args, **kwargs):
        super(NewAuthorForm, self).__init__(*args, **kwargs)
        # If you pass FormHelper constructor a form instance
        # It builds a default layout with all its fields
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        self.helper.add_input(Submit('save', 'save', css_class = 'btn-primary'))

http://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html#fundamentals