Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError at /post/politics Field 'id' expected a number but got 'politics'

I have a template 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 extends base2.html, because I want the posts.html to have nav bar functionality

<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>

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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})

>Solution :

Please try changing

<li><a href="politics">Politics</a></li>

with

<li><a href="{% url 'politics' %}">Politics</a></li>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading