hyeonga_code

파이선 웹구축_장고_33_투표 결과 확인 본문

Python_Django

파이선 웹구축_장고_33_투표 결과 확인

hyeonga 2023. 6. 25. 05:59
반응형

- 투표 결과 확인

1. 연산 설정 <config> > 'settings.py'

'settings.py'

=====

1
INSTALLED_APPS = [ 'mathfilters' ]
cs

2. 투표 결과 표시 <templates> > <vote> > 'detail.html'

- {% load mathfilters %} 추가

'detail.html'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<h1><b>DETAIL PAGE</b></h1>
<b>{{tp.subject}}</b> <br><br>
Please vote now!<br><br>
 
made by <b>{{tp.maker}}</b> <br><br>
<textarea cols='50' rows='4' disabled>{{tp.content}}</textarea><br><br>
 
{% load mathfilters %}
{% if user in tp.voter.all %}
    <h4>Vote Result</h4>
    {% for i in cset %}
        <b>{{i.name}}</b><br><br>
    {% endfor %}
    <a href="{% url 'acc:index' %}"><button type='button'>Main</button></a>
{% else %}
    <form method='post' action="{% url 'vote:vote' tp.id %}">
        #_ 인자 필요함
        {% csrf_token %}
        {% for i in cset %}
            <input type='radio' name='cho' value="{{i.id}}" {% if forloop.first %}checked{% endif %}>{{i.name}}
            #_ 처음에 체크되어 있음
        {% endfor %}
        <button>Vote</button>
        <a href="{% url 'acc:index' %}"><button type='button'>Main</button></a>
    </form>
{% endif %}
cs

>> + | floatformat:2 _ 백분율작업(자릿수)

 

 

최종 'index.html'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<h1><b>DETAIL PAGE</b></h1>
 
<b>{{tp.subject}}</b> <br><br>
Please vote now!<br><br>
 
made by <b>{{tp.maker}}</b> <br><br>
<textarea cols='50' rows='4' disabled>{{tp.content}}</textarea><br><br>
 
{% load mathfilters %}
{% if user in tp.voter.all %}
    <h4>Vote Result</h4>
    {% for i in cset %}
        <b>{{i.name}}</b> {{i.num | div:tp.voter.count|mul:100 | floatformat:2}} %<br><br>
    {% endfor %}
    <a href="{% url 'acc:index' %}"><button type='button'>Main</button></a>
{% else %}
    <form method='post' action="{% url 'vote:vote' tp.id %}">
        #_ 인자 필요함
        {% csrf_token %}
        {% for i in cset %}
            <input type='radio' name='cho' value="{{i.id}}" {% if forloop.first %}checked{% endif %}>{{i.name}}
            #_ 처음에 체크되어 있음
        {% endfor %}
 
        <button>Vote</button>
        <a href="{% url 'acc:index' %}"><button type='button'>Main</button></a>
    </form>
{% endif %}
cs

3. 메인 페이지에 참여한 투표에 표시 <templates> > <vote> > 'index.html'

'index.html'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<h1><b>VOTE PAGE</b></h1>
<b>{{user}}</b> Please vote now!<br><br>
<ul>
    {% for i in tp %}
        <li>
            <a href="{% url 'vote:detail' i.id %}">{{i}}</a>
            {% if user.name in tp.voter.all %}★{% endif %}
            #_ 왜 표시 안되는지?
        </li>
    {% endfor %}
</ul>
 
<a href="{% url 'acc:index' %}"><button>Main</button></a>
<a href="{% url 'acc:logout' %}"><button>Logout</button></a>
cs

 

반응형