1

I am using django-crispy-forms along with bootstrap to render my django forms.

I want to use a file upload field like here, but I'm not sure how to get my html to output like this:

<div class="fileupload fileupload-new" data-provides="fileupload">
  <div class="fileupload-new thumbnail" style="width: 50px; height: 50px;"><img src="http://www.placehold.it/50x50/EFEFEF/AAAAAA" /></div>
  <div class="fileupload-preview fileupload-exists thumbnail" style="width: 50px; height: 50px;"></div>
  <span class="btn btn-file"><span class="fileupload-new">Select image</span><span class="fileupload-exists">Change</span><input type="file" /></span>
  <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>

Using a ModelForm gives me an output like this:

<div id="div_id_my_id" class="control-group">
  <label class="control-label " for="id_my_id"> My Model Field</label>
  <div class="controls">
</div>

For a form like this:

class MyForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.layout = Layout(
            Div(
                Div('stuff', 'things' css_class='span6'),
                Div('main_image', 'my_id', css_class='span6' ),
            css_class='row-fluid'),
        )

    class Meta:
        model=MyForm

So Im going to need to add a div, a span, and change some of their attributes. Have you faced this problem? Thanks for your ideas!

1 Answer 1

4

You can do it this way:

from crispy_forms.layout import Field

class FileField(Field):
    template = 'bootstrap/layout/file_field.html'

file_field.html

{% load crispy_forms_field %}

<div id="div_{{ field.auto_id }}" class="control-group{% if form_show_errors%}{% if field.errors %} error{% endif %}{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}">
    {% if field.label %}
        <label for="{{ field.id_for_label }}" class="control-label {% if field.field.required %}requiredField{% endif %}">
            {{ field.label|safe }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
        </label>
    {% endif %}

    <div class="controls">
        <div class="fileupload fileupload-new" data-provides="fileupload">
          <div class="fileupload-new thumbnail" style="width: 50px; height: 50px;"><img src="http://www.placehold.it/50x50/EFEFEF/AAAAAA" /></div>
          <div class="fileupload-preview fileupload-exists thumbnail" style="width: 50px; height: 50px;"></div>
          <span class="btn btn-file"><span class="fileupload-new">Select image</span><span class="fileupload-exists">Change</span><input type="file" name="{{ field.html_name }}" id="id_{{ field.html_name }}" value="{{ field.value }}" /></span>
          <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
        </div>
        {% include 'bootstrap/layout/help_text_and_errors.html' %}
    </div>
</div>

And then use it in your FormHelper as FileField('main_image').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.