本文介绍了NoReverseMatch django基于类的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到这个错误
NoReverseMatch at / author / add / 4
反向'author_update'参数'()'和关键字参数'{'pk':39,'user_id':>}'未找到。尝试一种模式:['author /(?P \d +)/(?P \d +)$']
urls.py
url(r'^ author / add /(?P< user_id> \d +)$',AuthorCreate.as_view ='author_add'),
url(r'^ author /(?P< user_id> \d +)/(?P< pk> \d +)$',AuthorUpdate.as_view author_update'),
url(r'^ author /(?P< user_id> \d +)/(?P< pk> \d +)/ delete / $',AuthorDelete.as_view 'author_delete'),
url(r'^ author / list /(?P< user_id> \d +)$',AuthorList.as_view(),name ='author_list'),
models.py
类作者(models.Model):
subject = models.CharField(max_length = 100)
firstName = models.CharField(max_length = 100)
lastName = models.CharField(max_length = 100)
email = models.EmailField()
message = models.TextField()
created_by = models.F oreignKey(User)
def get_absolute_url(self):
return reverse('author_update',kwargs = {'pk':self.pk,'user_id':self.created_by})
views.py
code> class AuthorCreate(LoginRequiredMixin,CreateView):
model =作者
fields = ['subject','firstName','lastName','email','message',]
template_name ='authorCreate.html'
def form_valid(self,form):
form.instance.created_by = self.request.user
return super(AuthorCreate,self ).form_valid(form)
class AuthorList(LoginRequiredMixin,ListView):
template_name ='authorList.html'
queryset = Author.objects.all()
def get_context_data(self,** kwargs):
context = super(AuthorList,self).get_context_data(** kwargs)
if int(self.kwargs ['user_id'])! = self.request.user.id:
raise PermissionDenied
返回上下文
终端错误
内部服务器错误:/ author / add / 4
追溯(最近的最后一次呼叫):
文件/ home / ronald / best / A2 / 0124 /local/lib/python2.7/site-packages/django/core/handlers/base.py,第114行,get_response
response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/base.py,第69行,视图
return self.dispatch(请求,* args,** kwargs)
文件/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/utils/decorators.py,第29行,在_wrapper
返回bound_func(* args,** kwargs)
文件/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/contrib/auth /decorators.py,第22行,在_wrapped_view
中返回view_func(request,* args,** kwargs)
文件/ home / ronald / best / A2 / 0124 / local / lib / python2。 7 /
返回func(self,* args2,** kwargs2)
文件/ home / ronald / best / A2 / 0124 /vort/larb/views.py,第52行,调度
返回超级(LoginRequiredMixin,self).dispatch(request,* args,** kwargs)
文件/ home / ronald / best /A2/0124/local/lib/python2.7/site-packages/django/views/generic/base.py,第87行,调度
return handler(request,* args,** kwargs)
文件/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/edit.py,第205行,在帖子
返回超级(BaseCreateView,self).post(request,* args,** kwargs)
文件/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/通用/ edit.py,第171行,在帖子
中返回self.form_valid(form)
文件/home/ronald/best/A2/0124/vort/larb/views.py,行83,in form_valid
return super(AuthorCreate,self).form_valid(form)
文件/ home / ronald / best / A2 / 0124 / local / lib / p ython2.7 / site-packages / django / views / generic / edit.py,第148行,form_valid
return super(ModelFormMixin,self).form_valid(form)
文件/ home / /best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/edit.py,第78行,form_valid
return HttpResponseRedirect(self.get_success_url())
文件/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/edit.py,第136行,在get_success_url
url = self .object.get_absolute_url()
文件/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/utils/functional.py,第15行,_curried
return _curried_func(*(args + moreargs),** dict(kwargs,** morekwargs))
文件/home/ronald/best/A2/0124/local/lib/python2.7/site -packages / django / db / models / base.py,行1009,in get_absolute_url
return settings.ABSOLUTE_URL_OVERRIDES.get('%s。%s'%(opts.app_label,opts.model_name),func) (self,* args,** kwargs)
文件/ home / ronald / best /A2/0124/vort/larb/models.py,第59行,get_absolute_url
return reverse('author_update',kwargs = {'pk':self.pk,'user_id':self.created_by})
文件/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/core/urlresolvers.py,第509行,反向
return iri_to_uri( resolver._reverse_with_prefix(view,prefix,* args,** kwargs))
文件/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/core/urlresolvers .py,第429行,_reverse_with_prefix
(lookup_view_s,args,kwargs,len(patterns),patterns))
NoReverseMatch:对于'author_update'与参数'()'和关键字arguments'{ 'pk':39,'user_id':< SimpleLazyObject:< User:fdf>>}未找到。尝试了1个模式:['author /(?P< user_id> \\d +)/(?P< pk> \\d +)$']
解决方案
尝试更改此行 return reverse('author_update',kwargs = pk':self.pk,'user_id':self.created_by})
to:
return reverse('author_update',args =(self.pk,self.created_by.pk))
或
return reverse('author_update',
kwargs = {'pk':self.pk,'user_id' self.created_by.pk})
I get this error
NoReverseMatch at /author/add/4
Reverse for 'author_update' with arguments '()' and keyword arguments '{'pk': 39, 'user_id': >}' not found. 1 pattern(s) tried: ['author/(?P\d+)/(?P\d+)$']
urls.py
url(r'^author/add/(?P<user_id>\d+)$', AuthorCreate.as_view(), name='author_add'),
url(r'^author/(?P<user_id>\d+)/(?P<pk>\d+)$', AuthorUpdate.as_view(), name='author_update'),
url(r'^author/(?P<user_id>\d+)/(?P<pk>\d+)/delete/$', AuthorDelete.as_view(), name='author_delete'),
url(r'^author/list/(?P<user_id>\d+)$', AuthorList.as_view(), name='author_list'),
models.py
class Author(models.Model):
subject = models.CharField( max_length=100)
firstName = models.CharField( max_length=100)
lastName = models.CharField( max_length=100)
email = models.EmailField()
message = models.TextField()
created_by = models.ForeignKey(User)
def get_absolute_url(self):
return reverse('author_update', kwargs={'pk': self.pk, 'user_id': self.created_by})
views.py
class AuthorCreate(LoginRequiredMixin, CreateView):
model = Author
fields = ['subject', 'firstName', 'lastName', 'email', 'message',]
template_name = 'authorCreate.html'
def form_valid(self, form):
form.instance.created_by = self.request.user
return super(AuthorCreate, self).form_valid(form)
class AuthorList(LoginRequiredMixin, ListView):
template_name = 'authorList.html'
queryset = Author.objects.all()
def get_context_data(self, **kwargs):
context = super(AuthorList, self).get_context_data(**kwargs)
if int(self.kwargs['user_id']) != self.request.user.id:
raise PermissionDenied
return context
terminal error
Internal Server Error: /author/add/4
Traceback (most recent call last):
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/utils/decorators.py", line 29, in _wrapper
return bound_func(*args, **kwargs)
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in bound_func
return func(self, *args2, **kwargs2)
File "/home/ronald/best/A2/0124/vort/larb/views.py", line 52, in dispatch
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
return handler(request, *args, **kwargs)
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 205, in post
return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 171, in post
return self.form_valid(form)
File "/home/ronald/best/A2/0124/vort/larb/views.py", line 83, in form_valid
return super(AuthorCreate, self).form_valid(form)
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 148, in form_valid
return super(ModelFormMixin, self).form_valid(form)
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 78, in form_valid
return HttpResponseRedirect(self.get_success_url())
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 136, in get_success_url
url = self.object.get_absolute_url()
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/utils/functional.py", line 15, in _curried
return _curried_func(*(args + moreargs), **dict(kwargs, **morekwargs))
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/db/models/base.py", line 1009, in get_absolute_url
return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.model_name), func)(self, *args, **kwargs)
File "/home/ronald/best/A2/0124/vort/larb/models.py", line 59, in get_absolute_url
return reverse('author_update', kwargs={'pk': self.pk, 'user_id': self.created_by})
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 509, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/ronald/best/A2/0124/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 429, in _reverse_with_prefix
(lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'author_update' with arguments '()' and keyword arguments '{'pk': 39, 'user_id': <SimpleLazyObject: <User: fdf>>}' not found. 1 pattern(s) tried: ['author/(?P<user_id>\\d+)/(?P<pk>\\d+)$']
解决方案
Try to change this line return reverse('author_update', kwargs={'pk': self.pk, 'user_id': self.created_by})
to:
return reverse('author_update', args=(self.pk, self.created_by.pk))
or
return reverse('author_update',
kwargs={'pk': self.pk, 'user_id': self.created_by.pk})
这篇关于NoReverseMatch django基于类的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!