В django не первый день, но добавил get_absolute_url и абсолютно не понимаю что не так!
models
class Post(models.Model):
title = models.CharField(max_length=255)
body = models.TextField(max_length=500)
created_time = models.DateTimeField(auto_now=True)
def get_absolute_url(self):
return reverse('post', kwargs={'post_pk': self.pk})
urls
path('post/<int:post_pk>/', post_detail, name='post_detail'),
шаблон
{% for j in post %}
{{j.id}}
{{j.title}}<br>
{{j.body}}<br>
{{j.created_time}}
<a href="{{j.get_absolute_url}}">Подробнее</a>
<hr>
{%endfor%}
ошибка
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 4.1.7
Python Version: 3.8.9
Installed Applications:
['blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
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 C:\django-sites\django-tasks\tasks\blog\templates\blog\home_page.html, error at line 13
Reverse for 'post' not found. 'post' is not a valid view function or pattern name.
3 : {% block post%}
4 : <a href="{% url 'add_post' %}"><button type="button" class="btn btn-warning">Оставить статью</button></a>
5 :
6 :
7 : <div class="post">
8 : {% for j in post %}
9 : {{j.id}}
10 : {{j.title}}<br>
11 : {{j.body}}<br>
12 : {{j.created_time}}
13 : <a href=" {{j.get_absolute_url}} ">Подробнее</a>
14 : <hr>
15 : {%endfor%}
16 : </div>
17 :
18 : <style>
19 : .post{
20 : margin-top:20px;
21 : margin-left:50px;
22 : }
23 : </style>
Traceback (most recent call last):
File "C:\django-sites\django-tasks\env\lib\site-packages\django\core\handlers\exception.py", line 56, in inner
response = get_response(request)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\django-sites\django-tasks\tasks\blog\views.py", line 53, in home_page
return render(request, 'blog/home_page.html', dict)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\shortcuts.py", line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 1005, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 966, in render_annotated
return self.render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\loader_tags.py", line 157, in render
return compiled_parent._render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 1005, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 966, in render_annotated
return self.render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\loader_tags.py", line 63, in render
result = block.nodelist.render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 1005, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 966, in render_annotated
return self.render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\defaulttags.py", line 238, in render
nodelist.append(node.render_annotated(context))
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 966, in render_annotated
return self.render(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 1064, in render
output = self.filter_expression.resolve(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 715, in resolve
obj = self.var.resolve(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 847, in resolve
value = self._resolve_lookup(context)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\template\base.py", line 914, in _resolve_lookup
current = current()
File "C:\django-sites\django-tasks\tasks\blog\models.py", line 13, in get_absolute_url
return reverse('post', kwargs={'post_pk': self.pk})
File "C:\django-sites\django-tasks\env\lib\site-packages\django\urls\base.py", line 88, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "C:\django-sites\django-tasks\env\lib\site-packages\django\urls\resolvers.py", line 828, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'post' not found. 'post' is not a valid view function or pattern name.