Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

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

This issue only happens in production environment ,in my local development environment it works well.

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]+)$']

Production environment is digitalocean ubuntu 16.04.Development python version is 3.6,production python version is 3.5.

Here is my urls.py:

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

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

               ]

Here is 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"),
]

here is 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
    })

Here is the 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to help the people who may face the same issue.I need to answer this question.

I tried in my local machine it works well.But if I change a new database it occurs the same issue.

So I figure out that,this issue is caused by empty database!!! Just input something in your database it will work!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...