简体   繁体   English

根据属性显示对象

[英]Display objects based on attribute

This is very simple. 这很简单。 I have a database of objects with pin attributes. 我有一个带有pin属性的对象数据库。

use case 用例

  • user inputs 4-digit pin 用户输入4位数字引脚
  • displays objects with matching pin number 显示具有相同针号的对象

What is the simplest way to accomplish this in django? 在Django中最简单的方法是什么?

index.html index.html

<form action="/polls/" method="post">
    {% csrf_token %} 
    <p>
        <label for="pin">Enter group pin:</label> 
        <input id="pin" type="text" name="pin" maxlength="4" /> 
        <input type="submit" value="View Polls" />
    </p>
</form>

Currently it's hard coded in 目前,它已经硬编码

{% for poll in latest_poll_list %}
    {% if poll.pin == "1234" %}
        <ul class="poll-list">
            <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a> - {{poll.pin}}</li>
        </ul>
    {% endif %}
{% endfor %}

I am pretty new to django so there may be a better solution, but I'll give it a try. 我对django刚起步,因此可能有更好的解决方案,但我会尝试一下。 In your view you can do something like this. 在您看来,您可以执行以下操作。 Considering your code I assume you have a class Poll 考虑到您的代码,我假设您有一个课程Poll

poll = Poll.objects.filter(pin=request.POST['pin'])

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

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