samedi 29 novembre 2014

formulaires - POST de Django, faire quelque chose de mal avec < radio d'entrée > connecté < d'entrée texte > et booleanfield - Stack Overflow


I have a problem with Django 1.6


I have a questionnaire with multiple questions, that have chosen answers. To some of the chosen answers, a user can add extra text.


web template:


 {% for question in question %}
<label name='question{{question.id}}'>{{question.question}}</label>

{% for choiceanswer in question.choiceanswer_set.all %}
<input type="radio" name="question{{question.id}}" id="choice{{ choiceanswer.id }}" value="{{ choiceanswer.choice_text }}" />
<label for="choice{{ choiceanswer.id }}">{{ choiceanswer.choice_text }}</label>
{% if choiceanswer.add_empty_field == True %}
<input name='question{{question.id}}_custom' id="choice{{ choiceanswer.id }}" type='text'>
{% endif %}
{% endfor %}
{% endfor %}

view:


    questionsfilt = Question.objects.all
questionsdata = []
for question in questionsfilt:
name ='question' + str(question.id)
answer = request.POST[name] if name in request.POST else ''
name += '_custom'
answer_custom = request.POST[name] if name in request.POST else ''

questionsdata.append((question, answer, answer_custom))



return render(request, 'rendered_template.html',
{
'Question_data': questionsdata ,
'Question_answer': answer,
'Question_answer_custom': answer_custom,
})

the after POST template


      {% for question, Question_answer, Question_answer_custom in Question_data%}
<p>{{question.question}}</p>

<p>{{ Question_answer }}</p>

<p>{{ Question_answer_custom }}</p>


{% endfor %}

The problem: How could I hide {{ Question_answer_custom }} in 'after POST template', when in 'web template' it's equivalent the 'input name='question{{question.id}}_custom' has choiceanswer.add_empty_field == False.


If I use the following clause with question_answer_custom in in 'after POST template'


                {% if choiceanswer.add_empty_field == True %}
<p>{{ Question_answer_custom }}</p>
{% endif %}

then Question_answer_custom will be hidden, regardless if it's true or not.


And also how could I prevent Question_answer_custom overwriting itself.


Right now what I have something like the following example:



Question1?


1.Choice1: (add_empty_field == False)


2.Choice2: (if add_empty_field == True, show: input text>)


3.Choice3: (if add_empty_field == True, show: input text>)



If I select Choice2 in 'web template' and add my own choice into input text, the choice2 will render in 'after POST template' correctly, but it will show Choice3 input text value instead, what will be nothing.


Or if I pick Choice3 in 'web template', but switch to Choice1 afterwards, the Choice3 input text will still be displayed in 'after POST template', although Choice1 shouldn't have any text input.


My Question: How can I combine the input name='question{{question.id}}_custom' (after post 'answer_custom') field with the radio buttons, so the correct answer_custom field is displayed with the right answer in 'after POST template'. However, if the radio button add_empty_field is False in 'web template' it shouldn't be displayed at all in 'after POST template'?



I have a problem with Django 1.6


I have a questionnaire with multiple questions, that have chosen answers. To some of the chosen answers, a user can add extra text.


web template:


 {% for question in question %}
<label name='question{{question.id}}'>{{question.question}}</label>

{% for choiceanswer in question.choiceanswer_set.all %}
<input type="radio" name="question{{question.id}}" id="choice{{ choiceanswer.id }}" value="{{ choiceanswer.choice_text }}" />
<label for="choice{{ choiceanswer.id }}">{{ choiceanswer.choice_text }}</label>
{% if choiceanswer.add_empty_field == True %}
<input name='question{{question.id}}_custom' id="choice{{ choiceanswer.id }}" type='text'>
{% endif %}
{% endfor %}
{% endfor %}

view:


    questionsfilt = Question.objects.all
questionsdata = []
for question in questionsfilt:
name ='question' + str(question.id)
answer = request.POST[name] if name in request.POST else ''
name += '_custom'
answer_custom = request.POST[name] if name in request.POST else ''

questionsdata.append((question, answer, answer_custom))



return render(request, 'rendered_template.html',
{
'Question_data': questionsdata ,
'Question_answer': answer,
'Question_answer_custom': answer_custom,
})

the after POST template


      {% for question, Question_answer, Question_answer_custom in Question_data%}
<p>{{question.question}}</p>

<p>{{ Question_answer }}</p>

<p>{{ Question_answer_custom }}</p>


{% endfor %}

The problem: How could I hide {{ Question_answer_custom }} in 'after POST template', when in 'web template' it's equivalent the 'input name='question{{question.id}}_custom' has choiceanswer.add_empty_field == False.


If I use the following clause with question_answer_custom in in 'after POST template'


                {% if choiceanswer.add_empty_field == True %}
<p>{{ Question_answer_custom }}</p>
{% endif %}

then Question_answer_custom will be hidden, regardless if it's true or not.


And also how could I prevent Question_answer_custom overwriting itself.


Right now what I have something like the following example:



Question1?


1.Choice1: (add_empty_field == False)


2.Choice2: (if add_empty_field == True, show: input text>)


3.Choice3: (if add_empty_field == True, show: input text>)



If I select Choice2 in 'web template' and add my own choice into input text, the choice2 will render in 'after POST template' correctly, but it will show Choice3 input text value instead, what will be nothing.


Or if I pick Choice3 in 'web template', but switch to Choice1 afterwards, the Choice3 input text will still be displayed in 'after POST template', although Choice1 shouldn't have any text input.


My Question: How can I combine the input name='question{{question.id}}_custom' (after post 'answer_custom') field with the radio buttons, so the correct answer_custom field is displayed with the right answer in 'after POST template'. However, if the radio button add_empty_field is False in 'web template' it shouldn't be displayed at all in 'after POST template'?


0 commentaires:

Enregistrer un commentaire