Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

passing instance to Django modal form

I want to do edit on a modal pop up form, edit is working but cant see the existing instance, how I can do it? any help is appreciated. Here I have passed the form as below

class DomainListView(ListView):
    model = Domain
    template_name = 'super_admin/domain_list.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        form = DomainEditForm(self.request.POST or None)
        context['form'] = form
        return context

and the template is as follows

 <div class="modal fade" id="domain-edit-{{ item.pk }}">
          <div class="modal-dialog" role="document">
            <form class="form-valide" action="{% url 'domain_edit' item.pk %}" method="POST" id=""
                  enctype="multipart/form-data">
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title"> Edit Domain </h5>
                  <button type="button" class="close" data-dismiss="modal"><span>&times;</span>
                  </button>
                </div>
                <div class="modal-body">
                  <div class="basic-form">
                    <div class="form-group">
                         {% csrf_token %}
                          {{ form.errors }}
                          {{ form | crispy }}
                    </div>
                  </div>
                </div>
                <div class="modal-footer">
                  <button type="submit" class="btn btn-primary">submit</button>
                </div>
              </div>
            </form>
          </div>
        </div>

this is inside for loop along with list items, I tried to fetch the instance as follows in get context data override, but it gives key error

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    obj = get_object_or_404(Domain, kwargs={'pk': self.kwargs['pk']})
    form = DomainEditForm(self.request.POST or None, instance=obj)
    context['form'] = form
    return context
question from:https://stackoverflow.com/questions/65643039/passing-instance-to-django-modal-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...