0

I am new to Django and I am struggling to develop the following. I have a survey app in which an admin user can provide to create a new survey template a CSV with a variable number of questions and answers per question, which may be True or False indistinctly.

This is stored using these three different models:

class Survey(models.Model):
    survey_name = models.CharField(max_length=50, unique=True)
    answer_key = models.CharField(max_length=250)
    score_key = models.CharField(max_length=250)
    date = models.DateTimeField(auto_now_add=True)
    
class Question(models.Model):
    survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
    text = models.CharField(max_length=250)
    
class Answer(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    text = models.CharField(max_length=250)
    valid = models.BooleanField(default=False)

I know how to retrieve the associated objects for a given survey in a view and show them a template, but not how to represent them in a form, since the number of questions and answers per question are not known beforehand. Also not sure how this would be read when processing the POST.

Any hint would be much appreciated.

Thanks!

1 Answer 1

1

Finally I managed to do it without using a Django form, as per this post: get data from HTML without using django form template

As I am aware of the survey structure because I retrieve its components from querysets, I can create my customized form in the template assigning different values to the variable number of answers (answer1, answer2,... with a for loop) in a Multiple Choice form.

        <form class="pure-form pure-form-stacked" action={% url 'SETA_surveys:make_survey' slug %} method="post">
        {% csrf_token %}
        <fieldset>
            {% for question in questions %}             
                <label for="question">{{question.text}}</label>
                {% for answer in answers %}
                    {% if answer.question.id == question.id %}
                        <p><input type="checkbox" name="answer{{forloop.counter0}}"/> {{ answer.text }}</p> 
                    {% endif %}
                {% endfor %}
            {% endfor %}
            <br>
        <input class="pure-button pure-button-primary" type="submit" value="Submit">
        </fieldset>
        </form>

Then I get the answers in my view like this:

        if request.method == 'POST':
            user_answers = ''
            for i in range(len(survey_answers)):
                if 'answer'+str(i) in request.POST:
                    user_answers = user_answers+'1-'
                else:
                    user_answers = user_answers+'0-'
            user_answers = user_answers[:-1]

For sure it is not the cleanest way to go, but at least works for me :)

Thanks!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.