我尝试更新实例并返回实例的Template视图。
问题:代码呈现编辑视图但是当我编辑实例并按保存时,它会返回此错误。我意识到错误告诉我,我的Identity_nest_list
网址没有pk参数。但是我添加了它,它仍然给我错误。
基本上我希望能够编辑对象,保存编辑并将用户重定向到Identity_unique实例的更新版本,以及已经存在的其他实例
Request Method: POST
Request URL: http://127.0.0.1:8000/nesting/Identity-edit/L882394/?csrfmiddlewaretoken=umHqs06uQmn0fsBNPjnqpuv4yyBIPkGGJNmN1l83TLUSVFh3ja1WPd8reE3IvSEX
Django Version: 1.10.5
Python Version: 3.5.3
Installed Applications:
['Identities',
'nesting',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'widget_tweaks']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'Identity.middleware.LoginRequiredMiddleware']
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/edit.py" in post
240. return super(BaseUpdateView, self).post(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/edit.py" in post
183. return self.form_valid(form)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
163. return super(ModelFormMixin, self).form_valid(form)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
79. return HttpResponseRedirect(self.get_success_url())
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/generic/edit.py" in get_success_url
147. if self.success_url:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/functional.py" in __wrapper__
111. res = func(*self.__args, **self.__kw)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/urls/base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
392. (lookup_view_s, args, kwargs, len(patterns), patterns)
Exception Type: NoReverseMatch at /nesting/Identity-edit/L882394/
Exception Value: Reverse for 'Identity_nest_list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
以下是代码:
views.py
from django.shortcuts import render, redirect
from django.views.generic import TemplateView, UpdateView
from nesting.forms import Identity_Form, Symptom_Form
from nesting.models import Identity_unique, Symptom_relation
from django.core.urlresolvers import reverse_lazy
class Identity_view(TemplateView):
template_name = 'nesting/nesting.html'
def get(self, request):
form = Identity_Form()
Identities = Identity_unique.objects.filter(user = request.user)
var = {'form': form, 'Identities': Identities}
return render(request, self.template_name, var)
def post(self, request):
form = Identity_Form(request.POST)
content = None
if form.is_valid():
NIS = form.save(commit = False)
NIS.user = request.user
NIS.save()
content = form.cleaned_data['NIS']
form = Identity_Form()
return redirect('nesting:nesting')
var = {'form': form, 'content': content}
return render(request,self.template_name, var)
class Identity_nest_list_view(TemplateView):
model = Identity_unique
template_name = 'nesting/Identity_nest.html'
def get(self, request):
form = Identity_Form()
Identities = Identity_unique.objects.filter(user = request.user).order_by('-Timestamp')
var = {'form':form, 'Identities': Identities}
return render(request, self.template_name, var)
class Identity_unique_Update(UpdateView):
model = Identity_unique
fields = [ 'first_Name', 'last_Name', 'location', 'date_of_birth', 'contact',]
success_url = reverse_lazy('Identity_nest_list')
urls.py
from django.conf.urls import url
from nesting.views import Identity_view, Identity_nest_list_view, Symptoms_document_view, Medical_History_nest_view, Identity_unique_Update
from . import views
urlpatterns = [
url(r'^$', Identity_view.as_view(), name = 'nesting'),
url(r'^Identity-nest/(?P<pk>\w+)/$', Identity_nest_list_view.as_view(), name = 'Identity_nest_list'),
url(r'^Symptoms-document/(?P<pk>\w+)/$', Symptoms_document_view.as_view(), name = 'Symptoms_nest_list'),
url(r'^Symptom-view/(?P<pk>\w+)/$', Medical_History_nest_view.as_view(), name = 'Medical_History_nest'),
url(r'^Identity-edit/(?P<pk>\w+)/$', Identity_unique_Update.as_view(), name = 'Identity_unique_view_update')
]
Identity_nest.html
{% extends 'base.html' %}
<!DOCTYPE html>
<html>
{% block head %}
<head>
<meta charset="utf-8">
<title>See patient</title>
</head>
{% endblock %}
<body>
{% block body %}
<div class = "row" style = "margin-top: 80px;">
<div class="col-sm" >
<nav class =" " >
<ul class = "nav flex-column">
<li class = "nav-item"><a class = "nav-link" href = "{% url 'nesting:nesting'%}" ><small>Create Identity </small></a></li>
<li class = "nav-item"><a class = "nav-link" href = "#"><small>Move to Trash</small> </a></li>
</ul>
</nav>
</div>
<div class = "col-sm" >
{% for Identity in Identities %}
<div class = "card " style = "margin-top: 20px;" >
<ul class = "list-group list-group-flush">
<li class = "list-group-item"> <small class = "text-muted">Created On : {{Identity.Timestamp}}</small></li>
<li class = "list-group-item"><a class = "nav-link" href = "{% url 'nesting:Medical_History_nest' Identity.pk %}" >{{Identity.first_Name}} {{Identity.last_Name}} </a> <p>NIS: {{ Identity.NIS }}</p></li>
<li class = "list-group-item"><p> <small>Contact: {{ Identity.contact}}</small></p></a></li>
<li class = "list-group-item"><p> <small>Birthday: {{ Identity.date_of_birth}}</small></p></a></li>
<li class = "list-group-item"><a class = "nav-link" href = "{%url 'nesting:Symptoms_nest_list' Identity.pk %}" ><small>Create Medical State </small></a></li>
<li class = "list-group-item"><a class = "nav-link" href = "{%url 'nesting:Identity_unique_view_update' Identity.pk %}" ><small>Update</small></a></li>
</ul>
</div>
{% endfor %}
</div>
<div class="col-sm" ></div>
</div>
{% endblock %}
</body>
</html>
Identity_unique_form.html
{% extends 'base.html' %}
{% load widget_tweaks %}
<html>
<head>
{% block head %}
<title>Update</title>
{% endblock %}
</head>
<body>
{% block body %}
<style media="screen">
#navigation {
margin-top: 90px;
}
</style>
<div class = "container">
<div class = "row">
<nav class="col-md-3" style=" margin-top: 90px;">
<ul class = "nav flex-column">
<li class = "nav-item"> <a class = "nav-link" href = "{% url 'nesting:nesting'%}"><small>Create Identity</small> </a></li>
<li class = "nav-item"><a class = "nav-link" href = "{% url 'nesting:Identity_nest_list'%}" ><small>See Identity </small></a></li>
</ul>
</nav>
<main class = "col-md-7" style=" margin-top: 180px;">
<form method = 'post' novalidate>
{% csrf_token %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
<div class = "form-row">
<div class = "form-group col-md-6">
{{form.NIS.errors}}
{{form.NIS}}
</div>
<div class = "form-group col-md-6">
{{form.contact.errors}}
{{form.contact}}
</div>
</div>
<div class = "form-row">
<div class = "form-group col-md-6">
{{form.first_Name.errors}}
{{form.first_Name}}
</div>
<div class = "form-group col-md-6">
{{form.last_Name.errors}}
{{form.last_Name}}
</div>
</div>
<div class = "form-row">
<div class = "form-group col-md-6">
{{form.location.errors}}
{{form.location}}
</div>
<div class = "form-group col-md-6">
{{form.date_of_birth.errors}}
{{form.date_of_birth}}
</div>
</div>
<button class = "btn-primary btn-large btn ">Submit</button>
</form>
</div>
</main>
</div>
{% endblock %}
</body>
</html>
答案 0 :(得分:0)
Identity_unique_Update的成功网址不包含pk:
success_url = reverse_lazy('Identity_nest_list')
将其更改为:
def get_success_url(self):
return reverse('nesting:Identity_nest_list', kwargs={'pk': self.object.pk})