找不到带有参数“('',)”的“ news_detail”。尝试了1个模式:['news \\-(?P <news_pk> [0-9] +)$']

时间:2018-09-04 05:31:54

标签: python django django-templates django-views

此问题仅发生在生产环境中,在我的本地开发环境中效果很好。

Template error:
In template /home/william/kjmg/templates/base.html, error at line 0
   Reverse for 'news_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['news\\-(?P<news_pk>[0-9]+)$']
   1 : <!doctype html>
   2 : <html lang="en">
   3 : {% load staticfiles %}
   4 : <head>
   5 :     <!-- Required meta tags -->
   6 :     <meta charset="utf-8">
   7 :     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   8 : {#   导航栏图标显示 <link rel="icon" href="{% static 'images/logo-w.png' %}">#}
   9 :     <!-- Bootstrap CSS -->
   10 :     <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"

Traceback:

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
  89.         return handler(request, *args, **kwargs)

File "/home/william/kjmg/apps/news/views.py" in get
  68.             'opinion_news': opinion_news,

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/loader.py" in render_to_string
  62.     return template.render(context, request)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/template/defaulttags.py" in render
  447.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/urls/base.py" in reverse
  88.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

File "/home/william/kjmg/kjmgenv/lib/python3.5/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
  632.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'news_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['news\\-(?P<news_pk>[0-9]+)$']

生产环境为digitalocean ubuntu 16.04。开发python版本为3.6,生产python版本为3.5。

这是我的urls.py:

urlpatterns = [
               path('', NewsView.as_view(), name='index'),

               path('news', include(('news.urls', 'news'), namespace="news")),

               ]

这是news / urls.py:

from .views import newsDetailView

urlpatterns = [
    path('-<int:news_pk>', views.newsDetailView, name="news_detail"),
    path('tag_lists-<int:tag_pk>', views.tagNewsList, name="tag_news_list"),
    path('category_lists-<int:category_pk>', views.categoryNewsList, name="category_news_list"),
]

这是news / views.py

    class NewsView(View):
    """
    首页新闻列表
    """
    def get(self, request):

        all_news = News.objects.all().filter(select_news__isnull=True).exclude(category__name="opinion").order_by('-pk')

        return render(request, 'index.html', {
            'all_news': news,

        })


def newsDetailView(request, news_pk):
    news = get_object_or_404(News, id=news_pk)
    News.objects.filter(id=news_pk).update(pv=F('pv') + 1)
    relative_news = News.objects.filter(tag__id__in=news.tag.all()).exclude(id=news_pk)[:6]

    return render(request, "news_detail.html", {
        'news': news,
        'relative_news': relative_news
    })

这是index.html:

                    {% for news in all_news.object_list %}
                        <li class="media">
                            <a href="{% url 'news:news_detail' news.pk %}">
                                <img class="mr-3 rounded media-list-image" src="{{ MEDIA_URL }}{{ news.image }}"
                                     alt="Generic placeholder image">
                            </a>

1 个答案:

答案 0 :(得分:1)

为了帮助可能遇到相同问题的人们。我需要回答这个问题。

我尝试在本地计算机上运行良好。但是,如果我更改新数据库,则会出现相同的问题。

所以我发现,这个问题是由空数据库引起的!只需在您的数据库中输入内容即可!