简体   繁体   中英

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)
  • added 'CRISPY_TEMPLATE_PACK' to my settings
  • added 'crispy_forms' to my applications
  • done a 'collectstatic' from the downloaded tar file (since templates and CSS aren't included in the pip)
  • added '{% crispy_forms_tags %} to my form html file
  • imported from crispy_forms.helper and crispy_forms.layout to my forms.py

I'm using Bootstrap 3, which I'm coding directly rather than using bootstrap-toolkit

The Bootstrap renders fine on all my pages and so do the form fields, but the buttons won't appear.

My html is as follows:

{% 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. Rendered as above the form fields appear but not the button.

My forms.py looks like this:

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:

<!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. 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 :).

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. You're on the right track, you actually want to add the 'submit' button in init for the class, not within 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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