简体   繁体   中英

Display objects based on attribute

This is very simple. I have a database of objects with pin attributes.

use case

  • user inputs 4-digit pin
  • displays objects with matching pin number

What is the simplest way to accomplish this in django?

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. In your view you can do something like this. Considering your code I assume you have a class Poll

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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