django флажок получить значение флажка от html - PullRequest
0 голосов
/ 12 февраля 2020

html file

<form method="post">
        {% csrf_token %}
        <table border="2" bordercolor="black" width="500" height="100" cellspacing="0" cellpadding="5">
            <thead>
            <tr>
                <th>selection</th>
                <th>student name</th>
                <th>e-mail</th>
                <th>CV</th>
            </tr>
            </thead>
            <tbody>
            {% for ta in tas %}
                <tr>
                    <td><input type="checkbox" name="checkbox_list" value="{{ ta.id }}"></td>
                    <td>{{ ta.user.first_name }}&nbsp;{{ ta.user.last_name }}</td>
                    <td>{{ ta.user.email }}</td>
                    <td><a href="{{ ta.cv.url }}">view cv</a></td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
        <button type="submit">next</button>
    </form>

views.py

def ta_ranking(request, id):
    print('hello word!')
    check_box_list = request.POST.getlist('checkbox_list')
    if check_box_list:
        print(check_box_list)
        potential_ta = TA.objects.filter(id__in=check_box_list)
        return render(request, 'ta_ranking.html', {'potential_ta': potential_ta, 'course_id': id})
    return redirect('ta_list', id=id)

Мой вопрос заключается в том, что класс ta_ranking в views.py не вызывался, когда я нажимал кнопку отправки в html. Как я могу решить эту проблему? Спасибо за любую помощь!

1 Ответ

0 голосов
/ 12 февраля 2020

Вы не указали путь к представлению в своей форме:

<form method="POST" action="/path/to/ta_ranking">
...
</form>

Вам также необходимо добавить его в urls.py, если вы еще этого не сделали:

from django.urls import path
from myapp.views import ta_ranking

urlpatterns = [
    path('/path/to/ta_ranking', ta_ranking),
]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...