Python 与#x27相反;新闻日期';没有找到任何参数。尝试了1种模式:[';newsdate/(?P<;year>;[0-9]+;)$';]

Python 与#x27相反;新闻日期';没有找到任何参数。尝试了1种模式:[';newsdate/(?P<;year>;[0-9]+;)$';],python,django,Python,Django,我在尝试Django教程。 我使用了一个导航栏模板,并在基本html文件中使用了它。所有其他页面都继承了该基本文件。但是当链接newdate(在url.py中给出的名称)时,我得到了这个错误 NoReverseMatch at /newsdate/2020 Reverse for 'newsdate' with no arguments not found. 1 pattern(s) tried: ['newsdate/(?P<year>[0-9]+)$'] url.py fro

我在尝试Django教程。 我使用了一个导航栏模板,并在基本html文件中使用了它。所有其他页面都继承了该基本文件。但是当链接newdate(在url.py中给出的名称)时,我得到了这个错误

NoReverseMatch at /newsdate/2020

Reverse for 'newsdate' with no arguments not found. 1 pattern(s) tried: ['newsdate/(?P<year>[0-9]+)$']
url.py

from django.urls import path
from .views import news,contact,home,NewsDate

urlpatterns = [
    path('news/',news,name='news'),
    path('contact/',contact,name='contact'),
    path('newsdate/<int:year>',NewsDate,name='newsdate'),
    path('', home,name='home'),
]

从django.url导入路径
from.views导入新闻、联系人、主页、新闻日期
URL模式=[
路径('news/',news,name='news'),
路径('contact/',contact,name='contact'),
路径('newsdate/',newsdate,name='newsdate'),
路径(“”,home,name='home'),
]

视图的
路径
有一个
参数。所以您需要提供一个,例如:

<li><a href="{% url 'newsdate' 2020 %}">News by Date</a></li>

  • 好吧,
    {%url%}
    对于
    新闻日期
    ,它预计会有一年。
    <nav>
        <ul>
            <li><a href="{% url 'home' %}">Home</a></li>
            <li><a href="{% url 'news' %}">News</a></li>
            <li><a href="{% url 'contact' %}">Contact</a></li>
            <li><a href="{% url 'newsdate' %}">News by Date</a></li>
        </ul>
    </nav>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>{% block title%} {% endblock %}</title>
    
        {% include 'navbar.html' %}
    </head>
    <body>
        {% block body%} {% endblock %}
    </body>
    </html>
    
    {% extends 'base.html' %}
    {% block title %}Date Wise News{% endblock %}
    {% block body %}
    <h2>News for year {{year}}</h2>
    
    {% for article in atricle_list %}
    <h3>{{article.title}}</h3>
    <b>{{article.author}}</b>
    <b>{{article.pub_date}}</b>
    <p>{{article.description}}</p>
    
    {% endfor %}
    {% endblock %}
    
    from django.shortcuts import render
    from django.http import HttpResponse
    from .models import News
    from .models import SportNews
    # Create your views here.
    def news(request):
        context={
            "state":News.objects.get(id=1),
            }
        return render(request,'news.html',context)
    
    def home(request):
        return render(request,'home.html')
    
    def contact(request):
        return render(request,'contact.html')
    
    def NewsDate(request,year):
        artice_list=News.objects.filter(pub_date__year=year)
        context={
            'year':year,
            'artice_list':artice_list,
        }
        print("lol",artice_list)
    
        return render(request,'newsdate.html',context)
    
    
    from django.urls import path
    from .views import news,contact,home,NewsDate
    
    urlpatterns = [
        path('news/',news,name='news'),
        path('contact/',contact,name='contact'),
        path('newsdate/<int:year>',NewsDate,name='newsdate'),
        path('', home,name='home'),
    ]
    
    
    <li><a href="{% url 'newsdate' 2020 %}">News by Date</a></li>