Я пытался Django Учебники. Я использовал шаблон navbar и использовал его в базовом html файле. И все остальные страницы наследуют этот базовый файл. Но когда я связываю новую дату (имя указано в urls.py), я получаю эту ошибку
NoReverseMatch at /newsdate/2020
Reverse for 'newsdate' with no arguments not found. 1 pattern(s) tried: ['newsdate/(?P<year>[0-9]+)$']
Я посетил http://127.0.0.1: 8000 / newsdate / 2020 Моя база данных содержит один запись с датой 2020, выбор объектов из базы данных завершается как print ("lol", artice_list) печатает соответствующий элемент из базы данных.
Ошибка:
NoReverseMatch at /newsdate/2020
Reverse for 'newsdate' with no arguments not found. 1 pattern(s) tried: ['newsdate/(?P<year>[0-9]+)$']
Request Method: GET
Request URL: http://127.0.0.1:8000/newsdate/2020
Django Version: 3.0
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'newsdate' with no arguments not found. 1 pattern(s) tried: ['newsdate/(?P<year>[0-9]+)$']
Exception Location: /home/rishabh/anaconda3/lib/python3.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 676
Python Executable: /home/rishabh/anaconda3/bin/python
Python Version: 3.7.4
Python Path:
['/home/rishabh/Documents/DjangoWorks/Learning/src',
'/home/rishabh/anaconda3/lib/python37.zip',
'/home/rishabh/anaconda3/lib/python3.7',
'/home/rishabh/anaconda3/lib/python3.7/lib-dynload',
'/home/rishabh/anaconda3/lib/python3.7/site-packages']
Server time: Sun, 26 Apr 2020 17:11:09 +0000
Error during template rendering
In template /home/rishabh/Documents/DjangoWorks/Learning/src/templates/navbar.html, error at line 6
Reverse for 'newsdate' with no arguments not found. 1 pattern(s) tried: ['newsdate/(?P<year>[0-9]+)$']
1 <nav>
2 <ul>
3 <li><a href="{% url 'home' %}">Home</a></li>
4 <li><a href="{% url 'news' %}">News</a></li>
5 <li><a href="{% url 'contact' %}">Contact</a></li>
6 <li><a href="{% url 'newsdate' %}">News by Date</a></li>
7 </ul>
8 </nav>
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/newsdate/2020
Django Version: 3.0
Python Version: 3.7.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'NewsApp']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template /home/rishabh/Documents/DjangoWorks/Learning/src/templates/navbar.html, error at line 6
Reverse for 'newsdate' with no arguments not found. 1 pattern(s) tried: ['newsdate/(?P<year>[0-9]+)$']
1 : <nav>
2 : <ul>
3 : <li><a href="{% url 'home' %}">Home</a></li>
4 : <li><a href="{% url 'news' %}">News</a></li>
5 : <li><a href="{% url 'contact' %}">Contact</a></li>
6 : <li><a href=" {% url 'newsdate' %} ">News by Date</a></li>
7 : </ul>
8 : </nav>
Traceback (most recent call last):
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/rishabh/Documents/DjangoWorks/Learning/src/NewsApp/views.py", line 26, in NewsDate
return render(request,'newsdate.html',context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/loader_tags.py", line 188, in render
return template.render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 173, in render
return self._render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/template/defaulttags.py", line 443, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/urls/base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/rishabh/anaconda3/lib/python3.7/site-packages/django/urls/resolvers.py", line 676, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /newsdate/2020
Exception Value: Reverse for 'newsdate' with no arguments not found. 1 pattern(s) tried: ['newsdate/(?P<year>[0-9]+)$']
navbar. html
<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>
база. html
<!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>
новость. 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 %}
views.py
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)
urls.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'),
]