简体   繁体   English

Django:相关模型选择域

[英]Django: Dependent modelchoicefield

I have a bit of a problem. 我有一个问题。

I am pretty much new to django and I am having a problem in understanding on how I would make a modelchoicefield dependent to another modelchoicefield. 我对django相当陌生,在理解如何使modelchoicefield依赖于另一个modelchoicefield方面遇到问题。

I have been searching for a while now and came across with this . 我已经搜索了一段时间,并遇到了这个问题

Being a beginner, I tried the code as it is and fixed some very small issues but whenever I try to access the website (localhost) it downloads a text file that contains a single line of what appears to be a dictionary of some sort {1 : asd} and that's it, it doesn't display anything just downloads a text file. 作为一个初学者,我按原样尝试了代码,并修复了一些非常小的问题,但是每当我尝试访问网站(本地主机)时,它都会下载一个文本文件,其中包含单行,看起来像是某种形式的字典{1 : asd}就是这样,它不会显示任何内容,而只是下载一个文本文件。

I think it's because i have set the urls like this: 我认为这是因为我已将网址设置为:

url(r'^(?P<campus_id>[0-9]+)/$', views.get_schools, name='get_schools'),

What I think I am missing is: 我想我缺少的是:

Setting the url right and rendering or displaying of the fields 设置url权限并渲染或显示字段

I would really appreciate a bit of help, advice, or even a sample code. 我将非常感谢您的帮助,建议甚至是示例代码。

Thanks in advance. 提前致谢。

JavaScript is the best way to do this. JavaScript是执行此操作的最佳方法。

Filter selectbox options depending on primary choice 根据主要选择过滤选择框选项

Very useful to know in future. 以后了解非常有用。 I had a similar issue recently! 我最近有一个类似的问题!

Edit: I'm so sorry, this is an old question and you replied to it to say you've resolved it. 编辑:很抱歉,这是一个古老的问题,您回答了它说您已经解决了它。 Good job! 做得好!

I know this is a month old I tried studying javascript and turned out it was simple. 我知道这已经是一个月大了,我尝试学习javascript,结果发现它很简单。 Just filter and fill your dropdown through javascript like this 就像这样通过javascript过滤并填充您的下拉列表

        $(document).ready(function() {
         <!--The parent dropdown-->
            $("#id_REGION").change(function() {
                var el = $(this);
                var select = document.getElementById("id_CITY");
                $("#id_CITY").val([]);
                select.length = 0;
                $("#id_CITY").append("<option value=\"\" selected=\"selected\">---------</option>");
            <!--Clears the field for new entry-->

                var reg = [{% for item in a %}"{{ item.Region_Designated_id }}"{% if not forloop.last %},{% endif %}{% endfor %}];
                var city_name = [{% for item in a %}"{{ item.CITY }}"{% if not forloop.last %},{% endif %}{% endfor %}];

               <!--filling the dropdown-->
                for(var i = 0; i<reg.length; i++){

                <!--Filtering the entries-->
                    if(el.val() == reg[i]){
                        $("#id_CITY").append("<option id = "+ reg[i] +" value = \"" + city_name[i] + "\">" + city_name[i] + "</option>");
                    }

                }
            });
        });

This code uses two dropdowns making on dropdown dependent to another 此代码使用两个下拉菜单,使下拉菜单依赖于另一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM