alex
alex

Reputation: 2541

Django, can't make options selected

I'm trying to make the options in my select selected. In my code each user is giving scores to each other. I've tryed a few things but it doesn't work. The option should remain selected if the selected value is equal to the grade given to the user. I've used AbstractUser that's why there is User = get_user_model(), but I decided not to show it since it was useless.

models.py

class Score(models.Model):
    VALUE = (
        (1, "Score 1"),
        (2, "Score 2"),
        (3, "Score 3"),
        (4, "Score 4"),
        (5, "Score 5"),
        (6, "Score 6"),
        (7, "Score 7"),
        (8, "Score 8"),
        (9, "Score 9"),
        (10, "Score 10"),
    )
    granted_by = models.ForeignKey(settings.AUTH_USER_MODEL, default=0)
    granted_to = models.ForeignKey(settings.AUTH_USER_MODEL, default=0, related_name='granted_to')
    grade = models.PositiveSmallIntegerField(default=0, choices=VALUE)

    def __str__(self):
        return str(self.granted_to)

views.py

@login_required(login_url='/login/')
def vote(request):
    User = get_user_model()
    data = dict()
    data['users'] = User.objects.exclude(username=request.user)
    if request.method == "POST":
        if "rating" in request.POST:
            for key in request.POST:
                if 'grade_' in key:
                    awarded_grade = Score.objects.filter(granted_by=request.user,
                                                       granted_to__id=key.split('_')[1]).first()
                    if awarded_grade:
                        awarded_grade.grade = request.POST.get(key)
                        awarded_grade.save()
                    else:
                        Score.objects.create(granted_by=request.user,
                                               granted_to_id=key.split('_')[1],
                                               grade=request.POST.get(key))
    data['values'] = Score.VALUE
    data['grade'] = Score.objects.filter(granted_by=request.user)
    return render(request, 'vote.html', data)

vote.html

{% for user in users %}
    <tr>
        <td>{{ user.date_joined|date:"d.m.Y H:i" }}</td>
        <td>{{ user.username }} </td>
        <td>{{ user.id }}</td>
        <td>
            <select title="selecting_grade" name="grade_{{ user.id }}">
                {% for value in values %}
                    <option value="{{ value.0 }}" {% for a in grades %} {% if a.grade == value.0 %}selected{% endif %} {% endfor %} >{{ value.0 }}</option>
                {% endfor %}
            </select>
        </td>
    </tr>
{% endfor %}

I've tryed something like this in the console:

user_grade = Score.objects.filter(granted_by__username='Stalone', granted_to__username='Fane')
user_grade
<QuerySet [<Score: Fane>]>

for x in granted_user:
    print(x.grade)

2

or:

a = CustomUser.objects.all()
for user in a:
    grades = user.granted_to.values()
    print(grades)
<QuerySet [{'id': 22, 'grade': 5, 'granted_to_id': 14, 'granted_by_id': 1}]>
<QuerySet [{'id': 21, 'grade': 5, 'granted_to_id': 15, 'granted_by_id': 1}]>
<QuerySet [{'id': 19, 'grade': 5, 'granted_to_id': 16, 'granted_by_id': 1}]>
<QuerySet [{'id': 23, 'grade': 7, 'granted_to_id': 17, 'granted_by_id': 1}]>
<QuerySet [{'id': 20, 'grade': 8, 'granted_to_id': 18, 'granted_by_id': 1}]>
<QuerySet [{'id': 24, 'grade': 7, 'granted_to_id': 19, 'granted_by_id': 1}]>

To see if I can get the score given to each user and use them in selected, I got them, but I didn't manage to work with them, any suggestions?

Upvotes: 0

Views: 69

Answers (1)

Sathish Kumar VG
Sathish Kumar VG

Reputation: 2172

In views.py you are passing grade

data['grade'] = Score.objects.filter(granted_by=request.user)

But in vote.html, you are iterating with grades

<option value="{{ value.0 }}" {% for a in grades %} {% if a.grade == value.0 %}selected{% endif %} {% endfor %} >{{ value.0 }}</option>

Either change grade as grades in views.py or change grades as grade in vote.html

Upvotes: 1

Related Questions