Why I get error when I use href="{{ panel.get_absolute.url }}" in my templates?
I wrote a simple page to show a list of goods that when you click on each of them it forwards you to the purpose page to show the good's details, but I think this method doesn't work right now or I have a mistake ...
this is my model:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class PanelManager(models.Manager):
def ye(self,year):
return self.filter(publish__year=year)
def pu(self,publish):
return self.filter(status=publish)
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager,self).get_queryset().filter(status='published')
class Panel(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
author = models.ForeignKey(User, on_delete=models.CASCADE)
objects = PanelManager()
published = PublishedManager()
# objects = models.Manager()
def get_absolute_url(self):
return reverse('users:details',args=[self.slug, self.id])
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
my views:
from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse
from .models import Panel
def test1(request):
return HttpResponse("Hello this is test1")
def panellist(request):
panels=Panel.objects.filter(publish__year=2024)
return render(request, 'users/panel/panel list.html',{'panels':panels})
def p_details(request,panel, pk):
panel=get_object_or_404(Panel,slug=panel,id=pk)
return render(request, 'users/panel/details.html', {'panel':panel})
my urls:
from django.urls import path
from .import views
urlpatterns = [
path('panellist/', views.panellist, name='panellist'),
path('details/<slug:panel>/<int:pk>/', views.p_details, name='details'),
]
my template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test 2</title>
</head>
<body>
<h1>second test</h1>
{% for panel in panels %}
<a href="{{ panel.get_absolute_url }}">
<h2>{{panel.title}} : {{panel.author}} - {{panel.status}}</h2>
</a>
{% endfor %}
</body>
</html>
app installed:
enter image description here
my panel fields:
enter image description here
Then I got this error :
Error during template rendering
In template D:\Shahram\projects\New folder 4\D_newsite\templates\users\panel\panel list.html
, error at line 10
'users' is not a registered namespace
10 <a href="{{ panel.get_absolute_url }}">
This is the problem:
def get_absolute_url(self):
return reverse('users:details',args=[self.slug, self.id])
The error is telling you that it doesn't know about the prefix users:
in that url name.
Your urls.py
file contains a url named details
, but not users:details
.