简体   繁体   中英

Django. How can I add a widget for image input

How can I add a widget for image input.

Here code in file 'forms.py' in Django project

class BookForm(ModelForm):
class Meta:
    model = Book
    fields = ['name', 'img']
    widgets = {
        'name': TextInput(attrs={
            'class': 'form-control',
            'placeholder': 'Name'
        })
    }

Code in HTML:

<form method="post">
  {% csrf_token %}
     {{ form.name }}<br>
     {{ form.img }}
     <button type="submit" class="btn btn-success">submit</button>
    <span>{{ error }}</span>
 </form>

First of all, change your form in template .

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <input class="btn btn-success" type="submit" value="Submit">
</form>

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