'django: invalid syntax path('details/<int:id>/', views.details, name='details')

views.py file

def details(request, id):
post = Posts.objects.get(id=id)

context = {
    'post': post
}

return render(request, 'posts/details.html', context) 

urls.py file

from django.urls import path
from . import views

urlpatterns = [
   path('', views.index, name='index')
   path('details/<int:id>/', views.details, name='details')
]

path('details//', views.details, name='details') ^ SyntaxError: invalid syntax



Solution 1:[1]

urlpatterns = [
   path('', views.index, name='index'),
   path('details/<int:id>/', views.details, name='details')
]

Solution 2:[2]

There should be a comma after the first path statement Also just a suggestion :

I assume your app is called blog then there are two ways you can approach the above problem :

  1. Either declare a function which automatically generated a url from models which includes slug id etc etc or

  2. Make a get request from the html itself and then you don't need to specify after detail and you can directly capture url for id from views.py

Solution 3:[3]

Just put comma after each line!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jay
Solution 2 Amartya Gaur
Solution 3 Gaming Paradise