Как отобразить одну и ту же форму на нескольких страницах, используя один метод в django? - PullRequest
0 голосов
/ 04 марта 2020

У меня есть таблица с атрибутами, и я отображаю каждый атрибут как флажок в представлении html. Я хочу показать их на разных страницах, но я не хочу делать разные функции для каждой категории. Есть ли эффективный способ сделать это? Вот что я пробовал до сих пор.

def questions(request):
# start session page for the user to test
questions = Attribute.objects.all()
realistic = Attribute.objects.filter(holland_code=1)
investigative = Attribute.objects.filter(holland_code=2)
artistic = Attribute.objects.filter(holland_code=3)
social = Attribute.objects.filter(holland_code=4)
enterprising = Attribute.objects.filter(holland_code=5)
conventional = Attribute.objects.filter(holland_code=6)


left = [realistic, investigative, artistic, social, enterprising, conventional]
for attribute in left:
    # get all the values form the form submitted
    if request.method == "POST":
        # THIS WILL GET ALL THE RECOMMENDAITONS
        rAttributes = request.POST.getlist('realistic')
        print(rAttributes)
        return render(request, "main/questions.html", {"questions": attribute})

context = {
    "questions": realistic,

}
return render(request, 'main/questions.html', context)

Это мой html шаблон для отображения флажков

            <form action="" method="POST">
            {% csrf_token %}
            <div class="form-check">
                {% for question in realistic %}
                <input type="checkbox" class="form-check-input" id="exampleCheck1" name="realistics" value="{{ question.attribute_name }}">
                <label class="form-check-label" for="exampleCheck1">{{ question.attribute_name }}</label>
                <br>
                {% endfor %}  
            </div>

              <input type="submit" class="btn btn-danger" value="Next">
        </form>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...