didn’t match any of these.

Hello Guys,
I am developing a blogsite using django and i am new to django. I am getting an error

The current path, blog/{ % url 'article/'Post.pk %}, didn’t match any of these. 

my url.py

from django.urls import path
#from . import views 
from . views import PostView, ArticleView

urlpatterns = [
    #path('',views.blog,name="blog"),
    path('',PostView.as_view(),name='post'), # class based view url
    path('article/<int:pk>',name='article'),
    #path('',PostView.as_view()),
]

my blog.html

<h1>Welcome to the blog site</h1>
<ul>
{% for Post in object_list %}
   <li>
       <a href="{ % url 'article/'Post.pk %}">{{Post.post_title}} </a> - 
       {{Post.post_author}} </br>
    {{Post.post_body}} {{Post.pk}}</li>
{% endfor %} </ul>

my View.py

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post
#from blog import models
# Create your views here.
#def blog(request):
#   return render(request,'blog.html',{})

class PostView(ListView):
    model= Post
    template_name='blog.html'
    

class ArticleView(DetailView):
    model = Post
    template_name = 'articles_details.html'

I am not able find what i am missing? Thank you for the help !!

You are using the named path syntax and the “url” template tag, so your template tag must be like this:

<a href="{ % url 'article' Post.pk %}">{{ Post.post_title }} </a>

This is because with the “url” template tag you aren’t telling to Django that you will form a string, for example your path could be like this

path('anythingelse/<int:pk>',name='article'),

And eve so the syntax above should work because your are referring the “name” property of the path, not the path it self. This is the other way (hardconding the url):

<a href="/article/{{ Post.pk  }}">{{ Post.post_title }} </a>

In the last case you are building the url with a variable, the problem with that approach is that could be difficult to change and maintenance. Read this part of the Django Tutorial as reference.

Couple different things wrong here.

The problem in your template here is that you have a space between the { and the %, and you’re missing a space between the url name and the url parameter. It should read something like:
<a href="{% url 'article' Post.pk %}">{{Post.post_title}} </a>
No space between the { and %, and a space between the url name (which is “article”, not “article/”) and the parameter Post.pk.

You might want to review the docs on the url tag.

Thank you for the answer!!

Thank you for the answer!! I will do the following