发布数据时如何将 kwargs 传递给 url?我不断收到:没有 ReserveMatch 错误

问题描述 投票:0回答:0

我花了一整天的时间尝试但无法锻炼我做错了什么。我有两个视图,一个加载主视图,一个调用模式来发送电子邮件。当我通过模式发布数据时,出现反向匹配错误,但不确定原因。有趣的是,如果我在模式中清空一个字段,它不会出错,因为 pk 在那里。请告诉我我做错了什么。我知道有些人我没有将 pk 传回模板,但数据已发布,但你是怎么做到的?使用 get_form_kwargs 无效。

错误:找不到带有关键字参数“{'pk':None}'的'application-update'的反向。尝试了 1 种模式:['update/(?P[0-9]+)/$']

模板

{% url 'application-update' pk=application_update.id %}
{% url 'send-message' %}

urls.py

re_path(r'^update/(?P<pk>[0-9]+)/$', views.ApplicationViewUpdate.as_view(), name="application-update")
re_path(r'^send/$', views.SendMesssageView.as_view(), name="send-message"),
]

views.py

class ApplicationViewUpdate(UpdateView):
    model = Application
    form_class = ApplicationForm
    template_name = 'update_application.html'
    context_object_name = 'application_update'
    success_url = '/update/{id}/'

    def get_context_data(self, *args, **kwargs):
        context = super(ApplicationViewUpdate, self).get_context_data(*args, **kwargs)
        context['id'] = self.kwargs['pk']
        application = Application.objects.get(id = context['id'])
        context['message'] = MessageForm()
        return context


class SendMesssageView(FormView):
    form_class = MessageForm
    template_name = 'message.html'
    context_object_name = 'send_message'
    success_url = '/send/'

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['id'] = self.request.POST.get('id')
        context['pk'] = context['id']
        return context

    def form_valid(self, form):
        mailto = form.cleaned_data['mailto']
        mailfrom = form.cleaned_data['mailfrom']
        mailcc = form.cleaned_data['mailcc']
        mailsubject = form.cleaned_data['mailsubject']
        mailmessage = form.cleaned_data['mailmessage']
        form.send_mail(mailto, mailfrom, mailcc, mailsubject, mailmessage)
        return super(SendMesssageView, self).form_valid(form)

如果 mailto 字段是 [email protected],则从 SendMessageView 打印(上下文)

{'form': <MessageForm bound=False, valid=Unknown, fields=(mailto;mailfrom;mailcc;mailsubject;mailmessage)>, 'view': <views.SendMesssageView object at 0x7f9be8041490>, 'id': None, 'pk': None}

如果 mailto 字段是 [email protected],则从 SendMessageView 打印(上下文)

{'form': <MessageForm bound=True, valid=False, fields=(mailto;mailfrom;mailcc;mailsubject;mailmessage)>, 'view': <views.SendMesssageView object at 0x7f9c0a2e2ca0>, 'id': '6', 'pk': '6'}

template.html

<button class="btn" type="submit" value="notify" data-bs-toggle="modal" data-bs-target="#notify"><a>Notify</a></button>
<div class="modal fade" id="notify" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Notify</h5>
        </div>
        <form action="{% url 'send-message' %}" method="POST">
            {% csrf_token %}
            <div class="modal-body">
                <div class="row">
                    <div class="col">
                        <label for="{{ message.mailto.id_for_label }}">To:</label>
                        <input class="form-control" name="{{ message.mailto.html_name }}" id="{{ message.mailto.id_for_placeholder }}" value="">
                    </div>
                    <div class="col">
                        <label for="{{ message.mailcc.id_for_label }}">Cc:</label>
                        <input class="form-control" name="{{ message.mailcc.html_name }}" id="{{ message.mailcc.id_for_placeholder }}" value="">
                    </div>
                </div>
                <label for="{{ message.mailfrom.id_for_label }}">From:</label>
                <input class="form-control" name="{{ message.mailfrom.html_name }}" id="{{ message.mailfrom.id_for_placeholder }}" value="[email protected]">
                <label for="{{ message.mailsubject.id_for_label }}">Subject:</label>
                <input class="form-control" name="{{ message.mailsubject.html_name }}" id="{{ message.mailsubject.id_for_placeholder }}" value="You are in!!">
                <label for="{{ message.mailcc.id_for_label }}">Body:</label>
                <textarea class="form-control" name="{{ message.mailmessage.html_name }}" id="{{ message.mailmessage.id_for_placeholder }}" value="">
                   {% include 'message.html' %}
                </textarea>
            </div>
            <div class="modal-footer">
                <button type="Submit" class="btn btn-primary" name="id" value="{{id}}">Send</button>
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button>
            </div>
        </form>
      </div>
    </div>
</div>

<form action="{% url 'application-update' pk=application_update.id %}" method="POST">
{% include 'user_form.html' %}
</form>

django django-views django-forms django-templates django-urls
© www.soinside.com 2019 - 2024. All rights reserved.