我编写了一个简单的页面来显示商品列表,当您单击每个商品时,它会将您转发到目的页面以显示商品的详细信息,但我认为这种方法现在不起作用或者我犯了一个错误......
这是我的模型:
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
我的观点:
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})
我的网址:
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'),
]
我的模板
<!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>
应用程序已安装:
在此处输入图像描述
我的面板字段:
在此处输入图像描述
然后我收到这个错误:
模板渲染时出错
在模板中,第10D:\Shahram\projects\New folder 4\D_newsite\templates\users\panel\panel list.html
行出现错误
“用户”不是注册的命名空间
10 <a href="{{ panel.get_absolute_url }}">
这是问题所在:
该错误告诉您它不知道
users:
该 url 名称中的前缀。您的
urls.py
文件包含一个名为 的 URLdetails
,但不包含users:details
。