NoReverseMatch只有1个条目

时间:2015-03-11 18:58:26

标签: django

我似乎无法弄清楚这里发生了什么。我只获得一个NoReverseMatch只有一个项目,但所有其他类似的项目工作得很好。这是追溯。

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line  139, in get_response
    response = response.render()
  File "C:\Python27\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Python27\lib\site-packages\django\template\response.py", line 82, in rendered_content
    content = template.render(context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 140, in render
    return self._render(context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 134, in _render
    return self.nodelist.render(context)

  File "C:\Python27\lib\site-packages\django\template\base.py", line 840, in render
    bit = self.render_node(node, context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 854, in  render_node
    return node.render(context)
  File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line  123, in render
    return compiled_parent._render(context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 134, in _render
    return self.nodelist.render(context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 840, in render
    bit = self.render_node(node, context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 854, in     render_node
    return node.render(context)
  File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 840, in render
    bit = self.render_node(node, context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 854, in render_node
    return node.render(context)
  File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 305, in render
    return nodelist.render(context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 840, in render
    bit = self.render_node(node, context)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 854, in render_node
    return node.render(context)
  File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 203, in render
    nodelist.append(node.render(context))
  File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 447, in render
    six.reraise(*exc_info)
  File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 433, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
  File "C:\Python27\lib\site-packages\django\core\urlresolvers.py", line 509, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:\Python27\lib\site-packages\django\core\urlresolvers.py", line 429, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'john-view' with arguments '()' and keyword  arguments '{u'sam': 4321, u'john_version': u'.1'}' not found. 1 pattern(s)  tried: ['app/(?P<sam>\\d+)/john/view/(?P<john_version>\\d+\\.\\d+)/$']

以下是网址

url(r'^(?P<pk>\d+)/$',
        SamView.as_view(),
        name='Sam-view',
    )

url(r'^(?P<Sam>\d+)/John/view/(?P<John_version>\d+\.\d+)/$',
        JohnViewer.as_view(),
        name='john-view',
    )

任何类似条目的URL都是/app/123/ or /app/1234/ etc.但是对于此特定条目,它返回500错误(回溯来自日志)并且似乎正在拾取第二个URL,而它应该是拿起第一个 - 就像/app/4321一样。这两个URL都适用于其他所有内容。

如果您需要任何其他信息,请与我们联系。感谢。

这是SamView

class SamViewView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
    model = Samtbl
    template_name= 'John_details.html'
    permission_required = 'app.view_sam'

    @cache_control(no_cache=True, no_store=True, must_revalidate=True, max_age=0)
    def get(self, request, *args, **kwargs):
        return super(SamtView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(SamView, self).get_context_data(**kwargs)
        something =  
        Somethingtbl.objects.filter(samid=self.object.id).exclude(enabled=1)
        context['something'] = sorted(something, key=operator.attrgetter('number'))
        context['reports'] = JOhntbl.objects.filter(tid=self.object.id)
        for finding in context['something']:
             something.severity = ONE_CHOICES[something.severity]
             something.status = TWO_CHOICES[unicode(something.status)]
        return context

这是JohnViewer

class JohnViewer(LoginRequiredMixin, TemplateView):
    template_name = 'report_view.html'

    @cache_control(no_cache=True, no_store=True, must_revalidate=True, max_age=0)
   def get(self, *args, **kwargs):
        super(JohnViewer, self).get(*args, **kwargs)
        self.object = Samtbl.objects.get(id = self.kwargs['sam'])
        checker = ObjectPermissionChecker(self.request.user)
        if checker.has_perm('view_sam', self.object):
            if self.kwargs.has_key('pdf'):
                return self.pdf_view(self.request)
            context = self.get_context_data(**kwargs)
            if self.kwargs.has_key('draft'):
                self.template_name = 'john_draft.html'
                return self.pdf_view(self.request)
            return self.render_to_response(context)
        else:
            return HttpResponseForbidden()

    def get_context_data(self, **kwargs):
        context = super(JohnViewer, self).get_context_data(**kwargs)
        self.sam = Samtbl.objects.get(pk=self.kwargs['sam'])
        self.sam.type = SOME_CHOICES[self.sam.type]
        something_list = Somethingtbl.objects.filter(samid=self.sam.id).exclude(enabled=1)
        self.something = sorted(something_list, key=operator.attrgetter('number')) 
        for something in self.something:
            something.severity = SOME_CHOICES[something.severity]
            something.status = SOME_OTHER_CHOICES[something.status]
            something.details = set_inline_images(something.id, finding.details)
        user_name = self.sam.get_username()
        group_list = user_name.groups.values_list('name')
        for group_name in group_list:
            for vendor in VENDOR_LIST:
                if group_name[0] == vendor:
                    vendor_list = {'so-and-so'}
                    context['organization'] = vendor_list[vendor]
        context['sam'] = self.sam
        context['john'] = johntbl.objects.get(
                                version=self.kwargs['john_version'], 
                                tid=self.kwargs['sam'])
        context['something'] = self.something
        context['john_status'] = 'final'
        return context

sam-view模板中有很多链接。其中一个是约翰观 - <button><a href="{% url 'john-view' sam=samtbl.id john_version=john.version.strip %}" target="_blank">HTML</a></button>。没有“倒退”。在两个观点中的任何一个。

2 个答案:

答案 0 :(得分:0)

Sam get view应该是

def get(self, request,pk):
    print "PK",pk # here is your  id
    return super(SamtView, self).get(request, pk)

我不知道你为什么使用位置(* args)和命名参数(** kwargs)?你的网址没有捕获它们。您的网址只是传递了一个名为pk

的位置参数
url(r'^(?P<pk>\d+)/$',
        SamView.as_view(),
        name='Sam-view',
    )

答案 1 :(得分:0)

解决了这个问题。检查了数据库,John_version设置为.1。但john-view网址是

url(r'^(?P<Sam>\d+)/John/view/(?P<John_version>\d+\.\d+)/$',
    JohnViewer.as_view(),
    name='john-view',
    )

将其更改为0.1并且有效。