How to use django's admin "filter_horizontal" in my app ?

110 views
Skip to first unread message

Florian Supper

unread,
May 9, 2016, 11:10:02 AM5/9/16
to Django users
Hi @ all,

I'm looking for a way how to get the amazing function "filter_horizontal" which i use in django's admin site available in my bootstrap3 form.

I've searched some days, but i cant find no way to get it working..

So please could one of you help me!

#models.py
Class TestModel(models.Model):
    field1 = models.ManyToManyField('Contacts')
    field2 = models.ForeignKey('Testuser')

#forms.py
class TestForm(ModelForm):
    class Meta:
        model = TestModel
        fields = ['fiedl1','field2',]

#views.py
class CreateTestView(CreateView):
    form_class = TestForm
    template_name = 'myapp/test.html'

#test.html
{% block content %}
<br><br>

{% load bootstrap3 %}

<h2> Add a new entry </h2>

{% bootstrap_messages %}

    <form action="" method="post" class="form">
      {% csrf_token %}
      {% bootstrap_form form %}
      {% buttons %}
        <button type="submit" class="btn btn-primary">
          {% bootstrap_icon "star" %} Create
        </button>
      {% endbuttons %}
    </form>
    </div>

{% endblock %}

#urls.py
urlpatterns = [
     url(r'^test/add/$',  views..CreateTestView.as_view(),  name='create_test'),
]

Thanks for your help!

BR
Florian


Reply all
Reply to author
Forward
0 new messages