简体   繁体   English

django脆皮窗体按钮未显示

[英]django crispy form button not showing

I am sure that I'm being very dense, but have spent days trying to work out what I've done wrong. 我确信我非常密集,但是花了几天的时间来弄清楚我做错了什么。

I have 我有

  • installed django-crispy-forms (using pip) 安装的django-crispy-forms(使用pip)
  • added 'CRISPY_TEMPLATE_PACK' to my settings 在我的设置中添加了“ CRISPY_TEMPLATE_PACK”
  • added 'crispy_forms' to my applications 在我的应用程序中添加了“ crispy_forms”
  • done a 'collectstatic' from the downloaded tar file (since templates and CSS aren't included in the pip) 从下载的tar文件中完成了“ collectstatic”操作(因为pip中不包含模板和CSS)
  • added '{% crispy_forms_tags %} to my form html file 已将'{%crispy_forms_tags%}添加到我的表单html文件中
  • imported from crispy_forms.helper and crispy_forms.layout to my forms.py 从crispy_forms.helper和crispy_forms.layout导入到我的forms.py

I'm using Bootstrap 3, which I'm coding directly rather than using bootstrap-toolkit 我正在使用Bootstrap 3,我直接在编码而不是使用bootstrap-toolkit

The Bootstrap renders fine on all my pages and so do the form fields, but the buttons won't appear. Bootstrap在我的所有页面上都能很好地呈现,表单字段也可以呈现,但是按钮不会出现。

My html is as follows: 我的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>

If I change the crispy tag to {% crispy MyForm MyForm.helper %} I get 'VariableDoesNotExist at' plus a continuous of many (real) languages. 如果我将crispy标签更改为{%crispy MyForm MyForm.helper%},则会得到“ VariableDoesNotExist at”以及许多(真实)语言的连续形式。 Rendered as above the form fields appear but not the button. 如上所示,将显示表单字段,但不显示按钮。

My forms.py looks like this: 我的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'))

I know that there's some redundancy here in terms of imports, but figure that isn't the problem. 我知道在导入方面存在一些冗余,但是数字并不是问题。

Lastly, my base.html is: 最后,我的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>

I've put the crispy form templates in my templates folder, which is at the same level as manage.py. 我已经将松脆的表单模板放在模板文件夹中,该文件夹与manage.py处于同一级别。 As I say, all the bootstrap renders fine. 正如我所说,所有引导程序都可以正常显示。

I'm stumped. 我很沮丧 Any ideas gratefully received 感谢收到任何想法

I have absolutely no idea why, but somehow removing 我完全不知道为什么,但是以某种方式删除

from __future__ import absolute_import

from views fixed the issue for me and now my buttons are showing up. 从视图中解决了这个问题,现在我的按钮出现了。

PS Found this question, googling for a solution and decided to signup and hopefully help the next one with such a problem :). PS找到了这个问题,搜寻了一个解决方案,并决定注册并希望帮助下一个有此问题的人:)。

It's an old post, but if I made it here on my searches, it's likely someone else will. 这是一个旧帖子,但是如果我在搜索中在此发布它,很可能会有其他人这么做。

I'm using Django 1.11.9 and django-crispy-forms 1.7.0. 我正在使用Django 1.11.9和django-crispy-forms 1.7.0。 You're on the right track, you actually want to add the 'submit' button in init for the class, not within Meta. 您在正确的轨道上,实际上是要在类的init中而不是在Meta中添加“提交”按钮。

See below: 见下文:

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 http://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html#fundamentals

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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