将使用包含 select 标签的先前请求创建 Django 视图

发布于 2024-11-05 04:48:43 字数 2218 浏览 2 评论 0原文

我正在 Django+Python 中开发一个 web 应用程序。

我有三个 radio 按钮输入,它们的作用是显示和隐藏所需的 select 标签(在 Jquery 中完成)。即,通过检查其中一个单选选项,其相应的选择元素可见,其余元素则隐藏。

现在,选择元素的目的是选择价格范围,

<select name="price1" id="selPriceRange1" style="display:none" disabled > 
    <option value="1" >Below 400</option> 
    <option value="2" >400 to 600</option> 
    <option value="3" >600 to 1000</option> 
    <option value="4" >1000 to 1500</option> 
    <option value="5" >1500 to 2000</option> 
    <option value="6" >2000 to 2500</option> 
    <option value="7" >2500 to 4000</option> 
    <option value="8" >4000 to 7000</option> 
    <option value="9" >7000 to 15000</option> 
</select>

我为三个单选按钮提供了三个类似的选择范围。如果你能观察到,我给他们的值是'1','2',...现在我面临的问题是在提交数据时创建视图。

这是我的视图函数:

def search(request):
    if 'txtSearch' in request.GET and request.GET['txtSearch']:
        place = request.GET['txtSearch']
        typeOfPro = request.GET['property']
        typeOfPlace = request.GET['selToplace']

        if 'price1' in request.GET:
              price = request.GET['price1']
        elif 'price2' in request.GET:
          price = request.GET['price2']
        else:
          price = request.GET['price3']

        ghar = GharData.objects.filter(place__icontains=place, typeOfProperty__icontains=typeOfPro, typeOfPlace__icontains=typeOfPlace)
        return render_to_response('gharnivas/searchresult.html',{'ghar': ghar },context_instance=RequestContext(request))
    else:
        return render_to_response('gharnivas/ghar.html', {'error': True}, context_instance=RequestContext(request))

这里txtSearchproperty也是我提交的表单数据。

我面临的问题是如何检查 select 标记中的数据值?我的意思是,因为我想保留值“1”、“2”...并且如果数据库的实际值为 12000 美元作为值。那么我怎样才能做到这一点:

ghar = GharData.objects.filter(place__icontains=place, typeOfProperty__icontains=typeOfPro, typeOfPlace__icontains=typeOfPlace)

可以用一些东西作为比较吗?我应该这样做还是

如何将存储的数据与 数据库中存在的数据?

请问有人可以提供解决方案吗!

I am developing a webapp in Django+Python.

I have three radio button inputs, which are having the job of showing and hiding the required select tag's ( done in Jquery ). i.e. With the check of one of the radio option its respective select element is visible and the rest is hidden.

Now, with the select element having its purpose for selecting a price range

<select name="price1" id="selPriceRange1" style="display:none" disabled > 
    <option value="1" >Below 400</option> 
    <option value="2" >400 to 600</option> 
    <option value="3" >600 to 1000</option> 
    <option value="4" >1000 to 1500</option> 
    <option value="5" >1500 to 2000</option> 
    <option value="6" >2000 to 2500</option> 
    <option value="7" >2500 to 4000</option> 
    <option value="8" >4000 to 7000</option> 
    <option value="9" >7000 to 15000</option> 
</select>

I have three similar select ranges for three radio buttons. If you can observe, the value i have given to them is '1','2',... Now the problem i am facing is to create view when i submit the data.

Here is my view function:

def search(request):
    if 'txtSearch' in request.GET and request.GET['txtSearch']:
        place = request.GET['txtSearch']
        typeOfPro = request.GET['property']
        typeOfPlace = request.GET['selToplace']

        if 'price1' in request.GET:
              price = request.GET['price1']
        elif 'price2' in request.GET:
          price = request.GET['price2']
        else:
          price = request.GET['price3']

        ghar = GharData.objects.filter(place__icontains=place, typeOfProperty__icontains=typeOfPro, typeOfPlace__icontains=typeOfPlace)
        return render_to_response('gharnivas/searchresult.html',{'ghar': ghar },context_instance=RequestContext(request))
    else:
        return render_to_response('gharnivas/ghar.html', {'error': True}, context_instance=RequestContext(request))

Here txtSearch,property are also my form data that is submitted.

The problem I face is how will i check the value of the data from select tag? I mean since i thought of keeping the value '1','2',... And if the database had the actual value as $12000 as the value. Then how can i achieve this:

ghar = GharData.objects.filter(place__icontains=place, typeOfProperty__icontains=typeOfPro, typeOfPlace__icontains=typeOfPlace)

Can some thing be used as a comparison and should i do it or

how do I compare the stored data with
the data present in database?

Please if someone can provide a solution!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-11-12 04:48:43

由于您的数字与实际值不以任何数学方式对应,因此您需要简单地通过字典映射它们。然后您可以将该映射的结果传递给 __range 查询:

VALUE_MAP = {
    "1", (0, 399),
    "2", (400, 599),
    "3", (600, 1000),
    ...
}
value_range = VALUE_MAP[price]

GharData.objects.filter(...other criteria..., value__range=value_range)

Since your numbers don't correspond in any mathematical way to the actual values, you'll need to simply map them via a dict. Then you can pass the result of that map to a __range query:

VALUE_MAP = {
    "1", (0, 399),
    "2", (400, 599),
    "3", (600, 1000),
    ...
}
value_range = VALUE_MAP[price]

GharData.objects.filter(...other criteria..., value__range=value_range)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文