数据库中的Django动态下拉列表 [英] Django Dynamic Drop-down List from Database

查看:261
本文介绍了数据库中的Django动态下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个Django应用程序,其中一个功能我想要的是动态下拉列表...专门为车辆制造和模型...选择一个具体的make将只更新模型列表我认为这是可能的JavaScript或jQuery(这将是我最好的选择,如果有人有答案),但我不知道如何去做。

I wanted to develop a Django app and one of the functionalities I'd like to have is dynamic drop-down lists...specifically for vehicle makes and models...selecting a specific make will update the models list with only the models that fall under that make....I know this is possible in javascript or jQuery (this would be my best choice if anyone has an answer) but I don't know how to go about it.

另外,我想让make,model,year和系列是常见的,其他的属性,如颜色,传输等都是变量,所以只需要输入制造,型号,年份和系列只适用于新车。任何想法都将被高度赞赏。

Also, I'd want the make, model, year and series to be common then the other attributes like color, transmission etc to be variables so that one needs only enter the make, model, year, and series only for a new vehicle. Any ideas would be highly appreciated.

推荐答案

你提到的3件事情是常见的,make,model,year,将是3输入值。当提供给服务器时,包含详细信息的对象将被返回到调用页面。该页面将解析对象详细信息(使用JavaScript),并更新UI以将其显示给用户。

The 3 things you mention being common, make, model, year, would be the 3 input values. When given to the server, an object containing the details would be returned to the calling page. That page would parse the object details (using JavaScript), and update the UI to display them to the user.

从Django方面,需要有取3个输入,并返回输出。从客户端,需要有三个输入传递到服务器的功能,然后适当地解析服务器的响应。

From the Django side, there needs to be the facilities to take the 3 inputs, and return the output. From the client-side, there needs to be the facilities to pass the 3 inputs to the server, and then appropriately parse the server's response.

有一个REST api框架对于Django,这使得它更容易添加上述api - 活塞。使用活塞,您只需要为该资源创建一个URL,然后添加一个处理程序来处理它。 (您仍然需要浏览活塞文档,但这应该会让您了解它的外观)

There is a REST api framework for Django that makes it rather easy to add the "api" mentioned above -- Piston. Using Piston, you'd simply need to make a URL for that resource, and then add a handler to process it. (you'll still need to skim the Piston documentation, but this should give you an idea of what it looks like)

urls.py:
vehicle_details = Resource(handler=VehicleDetails)
url(r'^vehicle/(?<make>.*)/(?<model>.*)/(?<year\d{2,4}/(?P<emitter_format>[a-z]{1,4}), vehicle_details, name='vehicle_details'),

handler.py:
class VehicleDetails(BaseHandler):
    methods_allowed = ('GET',)
    model = Vehicles  #whatever your Django vehicle model is

    def read(self, request, *args, **kwargs):
        # code to query the DB and select the options
        # self.model.objects.filter()...            
        # Build a custom object or something to return

        return custom_object

只需设置url www.yoursite.com / vehicle / [make] / [model] / [year] / json返回JSON中的自定义数据对象进行jquery解析。

This simply sets up the url www.yoursite.com/vehicle/[make]/[model]/[year]/json to return a custom data object in JSON for jquery to parse.

在客户端,您可以使用jquery来设置一个事件(绑定),以便当所有3个下拉列表都被选中时,它将对api URL执行$ .get()。当它得到这个结果时,它将其传递到Jquery JSON解析器,并将自定义对象作为JavaScript对象。那个对象可以用来填充更多的下拉菜单。

On the client side, you could use jquery to setup an event (bind) so that when all 3 drop downs have a value selected, it will execute a $.get() to the api URL. When it gets this result back, it passes it into the Jquery JSON parser, and gives the custom object, as a javascript object. That object could then be used to populate more drop down menus.

(大警告,我刚刚写下了我的头顶,所以这不是要复制和粘贴,只是为了一般的想法。)

(Big warning, I just wrote the following off the top of my head, so it's not meant to be copy and pasted. It's just for the general idea.)

<script type="text/javascript">

    // On document load
    $(function() {
        $('#dropdown_make').bind('change', checkForValues());
        $('#dropdown_model').bind('change', checkForValues());
        $('#dropdown_year').bind('change', checkForValues());
    });

    function checkForValues() {
        if ($('#dropdown_make').val() && $('#dropdown_model').val() && $('#dropdown_year').val())
            updateOptions();        
    }

    function updateOptions() {
        url = '/vehicle/';
        url += $('#dropdown_make').val() + '/';
        url += $('#dropdown_model').val() + '/';
        url += $('#dropdown_year').val() + '/';
        url += 'json/';
        $.get(url, function(){
            // Custom data object will be returned here
        })
    }
</script>

这篇关于数据库中的Django动态下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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