question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bootstrap 4: Prettier File Inputs

See original GitHub issue

File inputs don’t look too great

image

They should look more like:

image

Ours:

<input type="file" name="photo" class="clearablefileinput" id="id_photo">

What it should be closer to:

<label class="custom-file">
  <input type="file" name="photo" id="id_photo" class="custom-file-input">
  <span class="custom-file-control"></span>
</label>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
hectormartinezdevcommented, Feb 19, 2019

Hey! I think that here is proper place to put this, even if its closed.

The template at crispy_forms/templates/bootstrap4/layouts/field_file.html, has this content on the branch master:

{% load crispy_forms_field %}

<div class="custom-file {{ field_class }}">
    {% crispy_field field 'class' 'custom-file-input' %}
    <label class="custom-file-label" for="{{ field.id_for_label }}">Choose file</label>
</div>

Which is great, but needs {% include 'bootstrap4/layout/help_text_and_errors.html' %} at the end to show the errors. So it will become:

{% load crispy_forms_field %}

<div class="custom-file {{ field_class }}">
    {% crispy_field field 'class' 'custom-file-input' %}
    <label class="custom-file-label" for="{{ field.id_for_label }}">Choose file</label>
    {% include 'bootstrap4/layout/help_text_and_errors.html' %}
</div>

I checked the dev branch and this file doesn’t exist, even the branch is more recent than master. On the network insight graph it shows that is 28 behind master and 5 ahead. But that belongs to another issue.

Should I open a new issue (and request) to add the fix? From which branch should I do it?

Thanks!

EDIT: forgot a ‘that’ before ‘belongs’.

2reactions
filippodaminatocommented, May 5, 2020

HI! I had the same problem today. I solved it as follows:

image_thumbnail.html


{% load crispy_forms_field %}
<!-- PREVIEW IMAGE -->
<div class="fileupload fileupload-new" data-provides="fileupload">
    <div class="row">
        <div class="col-md-12">
            <img id="output" class="img-thumbnail img-fluid w-100" {% if field.value %} src="{{ field.value.url }}"  {% endif %} />
            {% if field.value %}
                <input type="checkbox" name="{{ field.name }}-clear" id="{{ field.name }}-clear_id">
                <label for="{{field.name}}-clear_id">Clear</label><br>
            {% endif %}
        </div>
    </div>
</div>
<!-- INPUT -->
<div class="input-group mb-3">
    <div class="custom-file">
      <input type="file" name="{{field.name}}" accept="image/*" class="custom-file-input" id="inputGroupFile01" onchange="loadFile(event)" aria-describedby="inputGroupFileAddon01">
      <label class="custom-file-label" id="inputFileLabel">Presentation Image</label>
    </div>
</div>
{% include 'bootstrap4/layout/help_text_and_errors.html' %}

<script>
    // preview image
    var loadFile = function(event) {
      var output = document.getElementById('output');
      output.src = URL.createObjectURL(event.target.files[0]);
      output.onload = function() {
        URL.revokeObjectURL(output.src)
      }   
      document.getElementById("inputFileLabel").innerHTML = "{{field.label_tag}" + event.target.files[0].name;
    };
</script>

forms.py


from crispy_forms.layout import Field

class CustomImageField(Field):
    template = 'layout/fields/image_thumbnail.html'

forms.py


from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Row, Column

class CreateProductForm(forms.ModelForm):

    class Meta:
        model = Product
        fields = ( ...., 'img_presentation', .... )

    def __init__(self, *args, **kwargs):
        super(CreateProductForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        
        self.helper.layout = Layout(
           ....
            Row(
                Column(CustomImageField('img_presentation'), css_class='form-group col-md-12 '),
                css_class='form-row'
            ),
           ....
           Submit('submit', 'Crea',css_class='btn btn-block btn-success')
        )

Resutl 🎉

Screen Recording 2020-05-05 at 12 12 42

I hope that can be helpful to those in a similar situation

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plugins - Prettier
Plugins are ways of adding new languages or formatting rules to Prettier. Prettier's own implementations of all languages are expressed using the plugin...
Read more >
Form controls · Bootstrap v5.0
Give textual form controls like <input> s and <textarea> s an upgrade with custom styles, sizing, focus states, and more. ... Disabled file...
Read more >
Make the blog prettier with Bootstrap and Blacktie - FooBar
In this blog post we will make the blog look nicer, we will go from the basic non stylish view to a really...
Read more >
Formatting code with Prettier - GeeksforGeeks
Even if you have your old codebase, you can run Prettier on your codebase which will reformat all your massive code files in...
Read more >
Prettier not formatting HTML files in VS Code - Stack Overflow
defaultFormatter": "vscode.html-language-features" },. And try formatting again. This time Prettier will have better input to work with. The ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found