Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for use of layout with ModelForm #1018

Open
patagoniapy opened this issue Apr 18, 2020 · 8 comments
Open

Documentation for use of layout with ModelForm #1018

patagoniapy opened this issue Apr 18, 2020 · 8 comments

Comments

@patagoniapy
Copy link

Is there any documentation on use of crispy_forms.layout with ModelForms?

I would love to help write some examples and documentation for it, but can't seem to find any existing documentation or examples on customizing layout of model forms.

@smithdc1
Copy link
Member

Hi @patagoniapy

Thanks for your message. From a quick look I agree there isn't anything specific on model forms.

Here is a SO answer which gives some information. https://stackoverflow.com/a/13201588/12482183

If you have a suggestion on how to improve the documentation that would be great. 👍

@patagoniapy
Copy link
Author

patagoniapy commented Apr 18, 2020

I now you're very busy, but could you just briefly explain the differences between setting layout in the forms.ModelForm and forms.Form? For example, if I have a model like so:

class Book(model.Model):
     title = models.CharField(max_length=100)
     author = models.ForeignKey('author.name', on_delete=models.CASCADE)
     publisher = models.CharField(max_length=100)

I can create a model form:

from .models import Book

class BookForm(forms.ModelForm):
      class Meta:
               model = Book
               fields = ['title', 'author', 'publisher']

Now that the ModelForm is built, how could I for example set CSS, placeholder text, etc?

Once I understand this process, I wouldn't mind writing some ModelForm tutorials and/or examples for the documentation!

@patagoniapy
Copy link
Author

I actually just figured it out. When I have some examples ready, what is the best way to submit those for inclusion in the readthedocs ?

@smithdc1
Copy link
Member

smithdc1 commented Apr 19, 2020

I'm glad you worked it out 👍

The best way to propose some changes is via a pull request. The docs (which are hosted on rtd) are in this repo under the docs folder. You can build them locally by running the make file in the docs folder. ( Probably make html or make.bat html)

There are some notes in the contributing section of the docs which may help.

Good luck.

@ifo20
Copy link

ifo20 commented Jul 18, 2020

@patagoniapy how did you figure it out? I have followed the referenced SO answer, looked at all the docs I can find, and I still find my helper/layout is being completely ignored. my set up (none of the css classes are being applied anywhere on my form):

Django==3.0.7
django-crispy-forms==1.9.1

My form

class DeliveryOptionsForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(DeliveryOptionsForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_class = 'blue'
        self.helper.label_class = 'red'
        self.helper.css_class = 'yellow'

        self.helper.layout = Layout(
            Row(
                Field('city', wrapper_class='green-text'),
                Field('country', css_class='bg-santared mw-100'),
                css_class='form-row',
            )
        )

    class Meta:
        model = LetterAddress
        fields = [
            'address_line_1',
            'address_line_2',
            'city',
            'country',
            'postcode',
        ]

My template

<form
  method="POST"
  action="{% url 'core:delivery' %}" >
  {% csrf_token %}
  {{ form }}
</form>

My view

def delivery_options(request):
    letter_request_id = request.session.get('letter_request_id')
    letter_request = LetterRequest.objects.get(pk=letter_request_id)
    if letter_request.address is None: # address = models.ForeignKey(LetterAddress, null=True, on_delete=models.SET_NULL)

        letter_address = LetterAddress()
        letter_request.address = letter_address
        letter_address.save()
    if not request.POST:
        return render(request, "delivery.html", {
            'form': DeliveryOptionsForm(instance=letter_request.address),
        })
   # ... POST handling

@smithdc1
Copy link
Member

From a quick glance in your template you need to render the form using the crispy template tag.

So {{ form }} becomes {% crispy form %}

Will also need to load the tag in your template.

See - https://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html

Hope this helps.

@ifo20
Copy link

ifo20 commented Jul 18, 2020

@smithdc1 omg thank you i had gotten all confused reading the docs because crispy form errored on render for me ... I just realised it's because i was changing {{ form|crispy }} to {{ crispy form }} and not realising the {{ needed changing also ....

@smithdc1
Copy link
Member

Glad it worked for you.

Good luck with your project. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants