I have a template posts.html
{% extends 'base2.html' %}
{% block posts %}
<div >
<div >
<div >
<h1>{{posts.title}}</h1>
<h5>{{posts.created_at}}</h5>
<div >{{posts.body | safe}}</div>
</div>
</div>
</div>
{% endblock %}
posts.html extends base2.html, because I want the posts.html to have nav bar functionality
<nav id="navbar" >
<ul>
<li><a href="about">About</a></li>
<li><a href="guestposting">Guest Posting</a></li>
<li ><a href=""><span>Categories</span> <i ></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>
above is a section of the nav bar which is on base2.html, and also on the index.html. It works perfectly in the index.html. But when the user is on the posts.html-> path('post/str:pk', views.post, name='post') and they click politics category for instance, I get this error:
ValueError at /post/politics Field 'id' expected a number but got 'politics'.
Here are my url routes
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'),
So how can I make it possible such that when a user clicks on the politics category, they are redirected from the posts.html to the politics page->path('politics', views.politics, name='politics'),
My views.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})
CodePudding user response:
Please try changing
<li><a href="politics">Politics</a></li>
with
<li><a href="{% url 'politics' %}">Politics</a></li>
CodePudding user response:
Your post
view requires a pk
for the query. According to your url
path('post/<str:pk>', views.post, name='post'),
the url should be s.th. like /post/1
. Also note that the pk in your url definition needs to be an integer:
path('post/<int:pk>', views.post, name='post'),
If you just want to redirect the user on click of a category to that page you should just create an html anchor <a>
that points to the desired url like so:
<li><a href="/politics/">Politics</a></li>