1

I am using the following UpdateView in order to alter some values in the database:

class MyUpdateView(UpdateView):
    model = MyModel
    fields = ['VideoURL', 'MainDescription']
    template_name = 'myapp/edit.html'

This is my template:

    <div class="row">
        <div class="col-12 col-lg-7">
            <form method='post' class="card shadow-soft border p-4 mb-4">
                {% csrf_token %}
                <h5 class="mb-4">General</h5>
                <div class="form-group">
                    <label for="MainDescription">Title</label>
                    {{form.MainDescription|add_class:"form-control shadow-soft"}}
                </div>
                <div class="row">
                    <div class="col">
                        <button class="btn btn-primary btn-dark mt-2 animate-up-2"
                            type="submit">Update</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="col-12 col-lg-5">
            <form method='post' class="card shadow-soft border p-4 mb-4">
                {% csrf_token %}
                <div class="form-group">
                    <label for="VideoURL">Video URL:</label>
                    {{form.VideoURL|add_class:"form-control shadow-soft"}}
                </div>
                <div class="row">
                    <div class="col">
                        <button class="btn btn-primary btn-dark mt-2 animate-up-2 text-right"
                            type="submit">Update</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

However, I am facing some difficulties doing so because although both fields belongs to the same model, I cannot update any of the information. What I have noticed though is that if I place the both fields under the same HTML form, it works:

<div class="col-12 col-lg-5">
    <form method='post' class="card shadow-soft border p-4 mb-4">
        {% csrf_token %}
        <div class="form-group">
            <label for="VideoURL">Video URL:</label>
            {{form.VideoURL|add_class:"form-control shadow-soft"}}
        </div>
        <div class="form-group">
            <label for="MainDescription">Main Description:</label>
            {{form.MainDescription|add_class:"form-control shadow-soft"}}
        </div>
        <div class="row">
            <div class="col">
                <button class="btn btn-primary btn-dark mt-2 animate-up-2 text-right"
                    type="submit">Update</button>
            </div>
        </div>
    </form>
</div>

What am I missing here? Can anyone give me a hint? Thanks

Edit

It's due to how the forms are being displayed in the HTML form. They should be independent:

enter image description here

cdrrr
  • 1,213
  • 3
  • 11
  • 35
  • Why would you need two forms here, what kind of behavior are you trying to achieve? – Ivan Starostin Feb 01 '20 at 16:02
  • 1
    1) you can disengage "presentation" from "controller" and design "separate forms" handled by AJAX with one or many django views 2) you can make two forms for one model and handle them separately 3) take a look at similar questions like this one https://stackoverflow.com/questions/1395807/proper-way-to-handle-multiple-forms-on-one-page-in-django – Ivan Starostin Feb 01 '20 at 19:33

0 Answers0