How change value in SQL Django without entering in redactor

Question:

As an example I have models like this:

from django.db import models


class Pos(models.Model):
    POS_TAGS = [
        ('NOUN', 'NOUN'),
        ('ADJ', 'ADJ'),
        ('NUM', 'NUM'),
        ('PRON', 'PRON'),
        ('ADV', 'ADV'),
        ('VERB', 'VERB'),
        ('CNJ', 'CNJ'),
        ('ADP', 'ADP'),
        ('PRT', 'PRT'),
        ('INTJ', 'INTJ'),
        ('MOD', 'MOD'),
        ('IMIT', 'IMIT'),
        ('AUX', 'AUX'),
        ('PPN', 'PPN'),
        ('PUNC', 'PUNC'),
        ('SYM', 'SYM')
    ]
    token = models.CharField(max_length=150)
    pos = models.CharField(max_length=150, choices=POS_TAGS, null=True, default='NOUN')

and some data in Pos model:
enter image description here

t
Then I want to change data without entering in redactor:
Django redactor

like this:
enter image description here
Just change the value in the main page and press ok to save.

views.py:

from django.shortcuts import render
from .models import Pos

def index(request):
    pos = Pos.objects.all()
    return render(request, 'main/index.html', {'pos': pos})

template of redactor that should create:
First – it is a word (the token in PosModel)
Second – some values in dropdown to select and submit to database (the pos in PosModel)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Main</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div>
    {% for el in pos %}
    <ul class="list-group list-group-numbered">
        <li class="list-group-item d-flex justify-content-between align-items-start">
            <div class="ms-2 me-auto">
                <div class="fw-bold">{{ el.token }}</div>
            </div>
            <div class="btn-group">
                <button type="button" class="btn btn-danger">Action</button>
                <button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split"
                        data-bs-toggle="dropdown" aria-expanded="false">
                    <span class="visually-hidden">Toggle Dropdown</span>
                </button>
                <ul class="dropdown-menu">
                    <li><a class="dropdown-item" href="#">Action</a></li>
                    <li><a class="dropdown-item" href="#">Another action</a></li>
                    <li><a class="dropdown-item" href="#">Something else here</a></li>
                    <li>
                        <hr class="dropdown-divider">
                    </li>
                    <li><a class="dropdown-item" href="#">Separated link</a></li>
                </ul>
            </div>

            <button type="button" class="btn btn-outline-success">Submit</button>
        </li>
    </ul>
    {% endfor %}
</div>
</body>
</html>
Asked By: Ryadovoy Zadrot

||

Answers:

You have to have form inside html, otherwise you cannot perform POST action.
Add this to template:

{% for el in pos %}
    <form method="POST" id="pos_form_{{ el.id }}">
        {% csrf_token %}
        <input type="hidden" name="pos_id" value="{{ el.id }}">
        ...
        <button type="submit" class="btn btn-outline-success">Submit</button>
    </form>
    ...
{% endfor %}

And every value, that you want to be POSTable, you have to put inside <input ...> or <select ...>, like:

<select name="pos" form="pos_form_{{ el.id }}">
    {% for tag in pos_tags %}
        <option value="{{ tag.0 }}">{{ tag.1 }}</option>
    {% endfor %}
</select>

For the above example I assume that you will add pos_tags like that to the view’s context:

{..., 'pos_tags': Pos.POS_TAGS}

And whole view:

def index(request):
    if request.method == 'POST':
        pos = Pos.objects.get(id=int(request.POST['pos_id']))
        pos.pos = request.POST['pos']
        pos.save()

    pos = Pos.objects.all()
    return render(request, 'main/index.html', {'pos': pos, 'pos_tags': Pos.POS_TAGS})

Read more info about HTML Forms and Django Forms.

Answered By: NixonSparrow
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.