M
M
mrJill2016-10-04 13:31:30
Django
mrJill, 2016-10-04 13:31:30

Django dropdown in template. How is it customary to change objects correctly?

Good day!
Please don't drink too much.
I'm trying to master the first trial project in django (just getting to know each other).
I will be grateful for help.
#models.py

grade_list = (
    [1, 'Самый низкий']
    , [2, 'Низкий']
    , [3, 'Средний']
    , [4, 'Выше среднего']
    , [5, 'Высокий']
    , [6, 'Самый высокий']
)

class Request(models.Model):
    title = models.CharField(max_length=150)
    #....


class Grade(models.Model):
    user = models.ForeignKey(User)
    request = models.ForeignKey(Request)
    level = models.IntegerField(choices=grade_list, default=3)

#view.py
@login_required(login_url="/login/")
def requests(request):
    latest_requests_list = Request.objects.filter(active = True).order_by('average_grade', '-datetime')[:300]
    context = {'latest_requests_list': latest_requests_list, 'count_list': count_list}
    return render(request, "requests.html", context)

The template has a dropdown list
#....
        {% for request in latest_requests_list %}
        <tr>
            <td><a href="/requests/{{ request.id }}/"> {{ request.number }}</a></td>
            <td><a href="/requests/{{ request.id }}/">{{ request.datetime }}</a></td>
            <td><a href="/requests/{{ request.id }}/">{{ request.title }}</a></td>
            <td><a href="/requests/{{ request.id }}/">{{ request.actual_status }}</a></td>
            <td><a href="/requests/{{ request.id }}/">{{ request.str_av_grade }} ({{request.average_grade}})</a></td>
            <td>
                <ul class="dropdown">
                    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                        {{ request.cur_grade }}
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                        {% for avs in grade_list %} #я в курсе что список не передается. Это просто пример желаемой логики
                        <li><a href="#">{{avs}}</a></li>
                        {% endfor %}
                    </ul>
                </ul>
            </td>
    #....

How is it customary to pass a selected value from a list in a template to a model object in django?
Follow the link and set the value in the view (although the direct ways to change the data seem terribly kosher to me)?
I am sure that a simple and logical solution already exists.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question