/ providers / 121 / update /的NoReverseMatch 反向' provider_update'有参数'(无,)'未找到。尝试了1种模式:['提供者/(?P \ d +)/更新/']
我知道为什么抛出NoReverseMatch错误,因为url无法找到被识别的参数。但是,我不明白为什么我的参数没有被传递到正确的视图。当我传递一个包含参数ID或PK的var时,由于某种原因,它将其识别为None。我查看了类似的问题,但是他们没有将数据传递给表单操作的问题。任何帮助表示赞赏!!感谢
partial_provider_update.html
<form method="post" action="{% url 'provider_update' form.instance.id %}" class="js-provider-update-form">
{% csrf_token %}
<div class="modal-header">
<h4 class="modal-title">Update Provider</h4>
</div>
<div class="modal-body">
{% include 'providers/includes/partial_provider_form.html' %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Update provider</button>
</div>
</form>
partial_provider_list.html
{% for provider in object_list %}
<tr>
<td>{{ provider.id }}</td>
<td>{{ provider.name }}</td>
<td>{{ provider.organization }}</td>
<td>{{ provider.emr }}</td>
<td>{{ provider.date|date:"Y-m-d" }}</td>
<td style="width: 100px; vertical-align: center; padding-top: 9px">
<button type="button"
style="height: 25px; font-size: 16px;width: 50px; padding: 0px"
class="btn btn-warning btn-sm js-update-provider"
data-url="{% url 'provider_update' provider.id %}">
Edit
</button>
</td>
{% empty %}
<tr>
<td colspan="5" class="text-center bg-warning">No Providers</td>
</tr>
{% endfor %}
urls.py
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView, TemplateView
from providers.models import Provider
from providers import views
urlpatterns = [
url(r'^$', ListView.as_view(queryset=Provider.objects.all().order_by("id"), template_name='providers/home.html')),
url(r'^create/', views.provider_create, name='provider_create'),
url(r'^(?P<pk>\d+)/update/', views.provider_update, name='provider_update'),
]
views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render, get_object_or_404
from django.template.loader import render_to_string
from django.http import JsonResponse
from providers.models import Provider
from .forms import ProviderForm
def index(request):
return render(request, 'providers/home.html')
def save_provider_form(request, form, template_name):
data = dict()
if request.method == 'POST':
if form.is_valid():
form.save()
data['form_is_valid'] = True
object_list = Provider.objects.all()
data['html_provider_list'] = render_to_string('providers/includes/partial_provider_list.html', { 'object_list' : object_list })
else:
data['form_is_valid'] = False
else:
form = ProviderForm()
context = {'form': form}
data['html_form'] = render_to_string(template_name, context, request=request)
return JsonResponse(data)
def provider_create(request):
if request.method == 'POST':
form = ProviderForm(request.POST)
else:
form = ProviderForm()
return save_provider_form(request, form, 'providers/includes/partial_provider_create.html')
def provider_update(request, pk):
provider = get_object_or_404(Provider, pk=pk)
if request.method == 'POST':
form = ProviderForm(request.POST, instance=provider)
else:
form = ProviderForm(instance=provider)
return save_provider_form(request, form, 'providers/includes/partial_provider_update.html')
答案 0 :(得分:1)
问题出在save_provider_form()
函数中。
请参阅以下代码中的评论:
def save_provider_form(...):
if request.method == 'POST':
...
else:
...
else:
# ################### #
# HERE IS THE PROBLEM #
# ################### #
form = ProviderForm() # form is re-initialized with no instance
如果provider_update()
为request.method
,则在GET
函数中,您正在使用instance
初始化表单,但save_provider_form()
如果request.method
是GET
,您正在重新初始化没有实例的表单。因此,id
为None
,因为该实例为None
。