简体   繁体   中英

Horizontal view and Submit button in Crispy form Does not work

The Vertical Layout in crispy form is working by default but the horizontal layout for the form and Submit button are not appearing .. i think there is some problem with __init__ but the compiler shows it all right

views.py

class sessioncreate(LoginRequiredMixin,CreateView):

     model=Sessions
     #fields=['title','abstract','track','speaker']
     form_class=SessionForm

     def form_valid(self, form):
         form.save();
         return HttpResponseRedirect('/sessions')

form.py

from django.forms import ModelForm
from app.models import Sessions
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

class SesionsForm(ModelForm):

    class Meta:
        model=Sessions
        fields=['title','abstract','track','speaker']

    def __init__(self, *args, **kwargs):
        super(SessionForm, self).__init__(*args, **kwargs)
        self.helper=FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-sm-offset-1 col-sm-2'
        self.helper.field_class = 'col-sm-8'
        self.helper.add_input(Submit('submit', 'Submit'))

session_form.html

{% extends 'layout.html' %}

{% load crispy_forms_tags %}

{% block content %}
<form method="post">
    {% csrf_token %}
    {% crispy form %}
</form>
{% endblock %} 

I went through this MVA course too and encountered the same problem. I switched the bootstrap version to 4 instead of 3 in the settings.py file and made few changes in the forms.py file.

settings.py

CRISPY_TEMPLATE_PACK = 'bootstrap4'

forms.py

def __init__(self, *args, **kwargs):
    super(SessionForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_group_wrapper_class = 'row'
    self.helper.label_class = 'offset-md-1 col-md-1'
    self.helper.field_class = 'col-md-8'
    self.helper.add_input(Submit('submit', 'Submit'))

Not probably the answer you'd want, but I just wanted to share what worked for me.

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