[Django]-Crispy-forms InlineRadios don't show my model state

0👍

Turns out to have been a false alarm. My designer radically changed the css in a way that breaks crispy forms. I thought I had isolated the changes, but once I removed all the application generated CSS all worked well. Sorry for the false alarm.

0👍

There may be few errors:

  1. EntryForm has line model = Entry, instead must be model = Payment , as in model you describe Payment
  2. In EntryForm.__init__ lack of line super(EntryForm, self).__init__(*args, **kwargs)
  3. your __init__ declaration contain redundant(?) parameter request so declaration must be

    def __init__(self, *args, **kwargs): #without request
    

Please try this changes.

Your code(in my edition, below) works ok with normal behaviour of radio buttons, it normally creates new payment and prints existing.

models.py without changes

forms.py

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Fieldset, ButtonHolder, Submit
from crispy_forms.bootstrap import InlineRadios

class EntryForm(forms.ModelForm):
    class Meta:
        model = Payment # !not Entry
        fields = [
            'title', 'production_type'
        ]
    def __init__(self, *args, **kwargs):  #!without request
        super(EntryForm, self).__init__(*args, **kwargs)  #this line present
        self.helper = FormHelper()

        self.helper.layout = Layout(
            'title',
            InlineRadios('production_type'),
            Submit('submit', 'Submit'), # i miss your submit button, so adding here
        )

views.py

from django.views.generic.edit import CreateView


# it creates new payment 
class PaymentCreate(CreateView):
    model = Payment
    form_class= EntryForm
    template_name='payment_form.html'
    success_url = '/payment/'

# this usual view just print form with already created instance
def PaymentView (request):
    form =  EntryForm(instance = Payment.objects.get(id=4))
    c = {'form': form}
    return render(request,'payment_form.html', c)

payment_form.html

{% load crispy_forms_tags %}
<html>
<head>
</head>
<body>

    {% csrf_token %}
    {% crispy form %}

</body>
</html>

urls.py:

...
#i made 2 views, one for 'usual' view, one for generic view
url(r'payment_view/', PaymentView), 
url(r'payment/', PaymentCreate.as_view()),
...

Leave a comment