在Django中的管理员外部使用filter_horizo​​ntal最简单的方法 [英] Whats easiest way to use filter_horizontal outside of the Admin in Django

查看:144
本文介绍了在Django中的管理员外部使用filter_horizo​​ntal最简单的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非管理表单,我想使用filter_horizo​​ntal。我已经阅读过这个比我想要的(我只想要filter_horizo​​ntal)。我想检查一下是否有人想出了一个简单(更新的)方式来实现filter_horizo​​ntal。

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 to check to see if anyone has come up with a simpler (more current) way to just implement the filter_horizontal.

所以这里是代码:

class County(models.Model):
    """County Names"""
    name = models.CharField(max_length=64)
    state = USStateField(null=True)

class Company(models.Model):
    """The basics of a company"""
    name = models.CharField(max_length = 100)
    counties = models.ManyToManyField(County,blank=True, null=True)

然后我们的表单看起来像这样。我以为这会工作..

Then our form currently look like this. I thought this would work..

from django.contrib.admin.widgets import FilteredSelectMultiple
class RaterCompanyForm(ModelForm):
    class Meta:
        model = RaterOrganization
        exclude = ('remrate_projects',)
        widgets = {'counties': FilteredSelectMultiple(verbose_name="Counties",
                                                      is_stacked=True,) }
    class Media:
        css = {'all':['admin/css/widgets.css']}
        js = ['/admin/jsi18n/']

BTW:我明白这可能与,但他的问题没有回答。我做了很多功课 here 这里,但这两个都不起作用。

BTW: I understand this may be a duplicate of this but his question wasn't answered. I've done plenty of homework here and here but neither of these appear to work.

推荐答案

我知道这个线程很旧,但希望这个信息可以帮助别人绊倒这个页面,就像我一样。

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

经历了许多痛苦和痛苦之后,我得到了与Django 1.4的配合。像rh0dium一样,我尝试了所有这些文章,但不得不做很多调整。

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.

你不必对ModelForm做任何特别的事情,但你确实有在模板中包含所有这些js和css文件:

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:

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/core.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.init.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/SelectFilter2.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/SelectBox.js"></script>

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/widgets.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/base.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/forms.css"/>

然后按照通常的方式渲染表单,但需要将fieldset元素和类名称正确为css工作。例如:

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:

<fieldset>
    <div class="form-row">
        <form method="post" action=".">
            {% csrf_token %}
            {{ form.as_p }}
        <button type="submit" value="submit">Add</button>
    </form>
  </div>
</fieldset>

然后在模板的BOTTOM(在标记后呈现表单),添加此脚本替代pricetags与您的多对多(M2M)关系名称在模型表单的模型:

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:

<script type="text/javascript">
    addEvent(window, "load", function(e) { SelectFilter.init("id_pricetags", "pricetags", 0, "{{ STATIC_URL }}admin/"); });
</script>

显然您的媒体位置可能会有所不同,但{{STATIC_URL}}管理员/为我工作。

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

这篇关于在Django中的管理员外部使用filter_horizo​​ntal最简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆