【发布时间】:2022-08-23 19:46:54
【问题描述】:
我有一个模板posts.html
{% extends \'base2.html\' %}
{% block posts %}
<div class=\"row\">
<div class=\"leftcolumn\">
<div class=\"cardpost\">
<h1>{{posts.title}}</h1>
<h5>{{posts.created_at}}</h5>
<div class=\"fs-4\">{{posts.body | safe}}</div>
</div>
</div>
</div>
{% endblock %}
posts.html 扩展 base2.html,因为我希望 posts.html 具有导航栏功能
<nav id=\"navbar\" class=\"navbar\">
<ul>
<li><a href=\"about\">About</a></li>
<li><a href=\"guestposting\">Guest Posting</a></li>
<li class=\"dropdown\"><a href=\"\"><span>Categories</span> <i class=\"bi bi-chevron-down dropdown-indicator\"></i></a>
<ul>
<li><a href=\"tech\">Tech</a></li>
<li><a href=\"bizconomics\">Bizconomics</a></li>
<li><a href=\"politics\">Politics</a></li>
<li><a href=\"religion\">Religion</a></li>
<li><a href=\"sports\">Sports</a></li>
</ul>
</li>
<li><a href=\"contactform\">Contact</a></li>
</ul>
上面是导航栏的一部分,它位于 base2.html 和 index.html 上。它在 index.html 中完美运行。但是,当用户在 posts.html-> path(\'post/str:pk\', views.post, name=\'post\') 上并且他们单击政治类别时,例如,我收到此错误:
/post/politics 的 ValueError 字段 \'id\' 需要一个数字,但得到了 \'politics\'。
这是我的网址路线
path(\'\', views.index, name=\'index\'),
path(\'post/<str:pk>\', views.post, name=\'post\'),
path(\'politicalpost/<str:pk>\', views.politicalpost, name=\'politicalpost\'),
path(\'bizconomicspost/<str:pk>\', views.bizconomicspost, name=\'bizconomicspost\'),
path(\'techpost/<str:pk>\', views.techpost, name=\'techpost\'),
path(\'sportspost/<str:pk>\', views.sportspost, name=\'sportspost\'),
path(\'religionpost/<str:pk>\', views.religionpost, name=\'religionpost\'),
path(\'subscribe\', views.subscribe, name =\'subscribe\'),
path(\'contactform\', views.contactform, name=\'contactform\'),
path(\'about\', views.about, name=\'about\'),
path(\'guestposting\', views.guestposting, name=\'guestposting\'),
path(\'bizconomics\', views.bizconomics, name=\'bizconomics\'),
#These are the caregory urls
path(\'tech\', views.tech, name=\'tech\'),
path(\'sports\', views.sports, name=\'sports\'),
path(\'politics\', views.politics, name=\'politics\'),
path(\'religion\', views.religion, name=\'religion\'),
path(\'culture\', views.culture, name=\'culture\'),
path(\'culturepost/<str:pk>\', views.culturepost, name=\'culturepost\'),
那么我怎样才能使当用户点击政治类别时,他们从posts.html重定向到政治页面->路径(\'politics\',views.politics,name=\'politics\ '),
我的观点.py
def index(request):
politics = Politics.objects.all().order_by(\'-created_at\')
posts = Post.objects.all().order_by(\'-created_at\')
trendingposts = Trending.objects.all().order_by(\'-created_at\')
religions = Religion.objects.all().order_by(\'-created_at\')
sliders = Heroslider.objects.all()
return render(request,
\'index.html\',
{\'politics\':politics, \'posts\':posts,\'trendingposts\':trendingposts,\'religions\':religions, \'sliders\':sliders})
def politics(request):
politics = Politics.objects.all()
return render(request, \'Politics/politics.html\', {\'politics\':politics})
def post(request, pk):
posts = Post.objects.get(id=pk)
return render(request, \'posts.html\', {\'posts\':posts})