regular expression in an url showing post details

30 views
Skip to first unread message

Tiempo

unread,
May 25, 2020, 8:50:35 PM5/25/20
to Django users
Hi,
my name is Tiempo and I'm new with  Django. currently, I'm using django 3.0 and i want to display each article of a blog app ( like when I click on article 1, it should display it full details on page show with the following link http://127.0.0.1:8000/blog/posts/1/)

containing a parameter named id (of an article) but the path is not recognized : 
urlpatterns = [

path('', views.index, name='Blog'),
path('posts/?P<ids>[0-9]+', views.show),


This is how I called the function show in views:
def show(request, id):
return render(request, 'blog/show.html', {'id': id})


This is what happens when I execute the local server

Page not found (404)

Request Method:GET
Request URL:http://127.0.0.1:8000/blog/posts/1/

Using the URLconf defined in monsite.urls, Django tried these URL patterns, in this order:

  1. admin/
  2. [name='Home']
  3. about/ [name='About']
  4. contact/ [name='Contact']
  5. blog/ [name='Blog']
  6. blog/ posts/?P<ids>[0-9]+

The current path, blog/posts/1/, didn't match any of these.


can someone help me with how to write the regular expression in Django 3.0, please?




Ousseynou Diop

unread,
May 26, 2020, 10:48:56 AM5/26/20
to Django users
Hello friend the error is you've used a different name for regex.
Try to change "ids" in URLs to "id".
Example: path('posts/?P<id>[0-9]+', views.show),

Or easily with this : path("posts/<int:id>/", views.show)

Daniel Roseman

unread,
May 26, 2020, 11:02:47 AM5/26/20
to Django users
What gave you the impression you should be using regular expressions? `path` does not take regex and you should not be using them in Django 3.0. 

The correct syntax is:

    path('posts/<int:id>/', views.show),

--
DR.
Reply all
Reply to author
Forward
0 new messages