Whats easiest way to use filter_horizontal outside of the Admin in Django

后端 未结 2 457
南笙
南笙 2020-12-29 10:03

I have a non-admin form in which I\'d like to use filter_horizontal on. I have read this which does much more than what I want (I only want the filter_horizontal). I wanted

2条回答
  •  佛祖请我去吃肉
    2020-12-29 10:36

    I know this thread is old, but hopefully this info will help someone else who stumbles upon this page like I did.

    After much pain and suffering, I was able to get this to work with Django 1.4. Like rh0dium, I tried all those articles, but had to make a lot of tweaks.

    You don't have to do anything special with the ModelForm, but you do have to include all these js and css files in the template:

    
    
    
    
    
    
    
    
    
    
    

    Then render the form as you normally would, but you need to get the fieldset elements and class names right for the css to work. For example:

    {% csrf_token %} {{ form.as_p }}

    Then at the BOTTOM of the template (after the markup to render the form), add this script and replace pricetags with whatever your Many to Many (M2M) relationship name is on the model form's model:

    
    

    Apparently your media location may be something different, but {{ STATIC_URL }}admin/ worked for me.

提交回复
热议问题