G
G
german-ozerman2018-02-11 10:00:09
Django
german-ozerman, 2018-02-11 10:00:09

Django creating a poll, how to get and pass value (user response)?

I am making a questionnaire,
looping through the table with questions (Metrix), looking for a way to save and return the values ​​of the answers value = '' (Metrix_user) on the same page for editing by the user online. (in the future I plan to use pagination with back buttons next)
**My models:**

from django.db import models
    from django.conf import settings
    

    class Metrix(models.Model):
        title = models.CharField(max_length=256, verbose_name='Question')
        metrix_category = models.ForeignKey(
            'category',
            related_name='Question_category',
            on_delete=models.CASCADE,
            verbose_name='Category',
        )
        is_published = models.BooleanField(default=False)
        def __str__(self):
            return self.title
    
    class Category(models.Model):
        title = models.CharField(max_length=256, 
            verbose_name='Question_category')
        is_published = models.BooleanField(default=False)
        def __str__(self):
            return self.title
    
    class User_metrix(models.Model):
        user = models.ForeignKey(
            settings.AUTH_USER_MODEL, 
            on_delete=models.CASCADE, 
            related_name="user_metrix", 
            verbose_name='User')
        metrix = models.ForeignKey('Metrix', on_delete=models.CASCADE, 
            verbose_name='Question')
        value = models.DecimalField(max_digits=12, decimal_places=2, 
            verbose_name='Value')

**My view:**
from django.shortcuts import render
    from django.contrib.auth.decorators import login_required
    from metrix.models import Metrix, User_metrix

    @login_required
    def metrix_view(request, pk=None):
        metrix_category = {
            'pk': 4
        }
        
        #Get questions by category
        metrix_list = Metrix.objects.filter(is_published=True,  
             metrix_category__pk=pk)

        context = {
            'metrix_list': metrix_list
        }    
        return render(request, 'metrix/metrix.html', context)

**Template:**
Getting questions in the "metrix_list" loop from the Metrix table Looking
for a way to save the value value and return it to the user (this value should be stored in the Metrix_user table) the problem is that in metrix_list - only questions according to the data model
<!--cycle for metrix-->
        {% for item in metrix_list %}

        <div class="row metrix_quiestion_line justify-content-center">
          <div class="metrix_quiestion">
            <h2>
            {{ item }}
            </h2>
          </div>
          <div class="metrix_value">
            <input type="number" name="{{ item.id }}" value=" ">
          </div>
        </div>

        {% endfor %}

        <!--END cycle -->

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ramon Yaskal, 2018-02-12
@ramonyaskal

See how the questionnaire is implemented here Code
Demo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question