Update data



  • I have a HTML code that's in charge of updating staff data. Box 1 (inspect the photo) presented by the text field updates the data. But box two and three, which presents data in the form of a deleting list (data from the model) does not update the changed data.

    For convenience, code:

    <form method="POST">
        {% csrf_token %}
        <p>
            <label>Введите имя</label><br>
            <input type="text" name="name" value="{{person.name}}" />
        </p>
    
    &lt;p&gt;
        &lt;label&gt;Введите отдел&lt;/label&gt;&lt;br&gt;
        &lt;select&gt;
            {% for key in dep %}
               &lt;option value="{{ key }}"&gt;{{ key }}&lt;/option&gt;
            {% endfor %}
        &lt;/select&gt;
    &lt;/p&gt;
    
    &lt;p&gt;
        &lt;label&gt;Введите должность&lt;/label&gt;&lt;br&gt;
        &lt;select&gt;
            {% for key in pos %}
               &lt;option value="{{ key }}"&gt;{{ key }}&lt;/option&gt;
            {% endfor %}
        &lt;/select&gt;
    &lt;/p&gt;
    
    
    &lt;input type="submit" value="Сохранить" &gt;
    

    </form>

    And a picture to show.
    введите сюда описание изображения введите сюда описание изображения

    views.py

    def edit(request, id):
    try:
    person = Employee.objects.get(id=id)
    dep = Department.objects.all()
    pos = Position.objects.all()

    if request.method == "POST":
        person.name = request.POST.get("name")
        person.save()
        return redirect("base")
    else:
        return render(request, "manager_employees/edit.html", {"person": person,
                                                               'dep': dep,
                                                               'pos': pos})
    

    except Employee.DoesNotExist:
    return HttpResponseNotFound("<h2>Person not found</h2>")

    What do I have to do with the data?



  • Look. You're getting from the input. name♪ You need to get in. html

    <form method="POST">
        {% csrf_token %}
        <p>
            <label>Введите имя</label><br>
            <input type="text" name="name" value="{{person.name}}" />
        </p>
    
    &lt;p&gt;
        &lt;label&gt;Введите отдел&lt;/label&gt;&lt;br&gt;
        &lt;select name='select1'&gt;
            {% for key in dep %}
               &lt;option value="{{ key }}"&gt;{{ key }}&lt;/option&gt;
            {% endfor %}
        &lt;/select&gt;
    &lt;/p&gt;
    
    &lt;p&gt;
        &lt;label&gt;Введите должность&lt;/label&gt;&lt;br&gt;
        &lt;select  name='select2'&gt;
            {% for key in pos %}
               &lt;option value="{{ key }}"&gt;{{ key }}&lt;/option&gt;
            {% endfor %}
        &lt;/select&gt;
    &lt;/p&gt;
    
    
    &lt;input type="submit" value="Сохранить" &gt;
    

    </form>

    to select name
    And then get them as an input.

    person.select1 = request.POST.get("select1")
    person.select2 = request.POST.get("select2")

    and keep them in the model. Just like you did.

    person.name = request.POST.get("name")



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2