'() missing 1 required positional argument: 'id'

I keep getting an error when trying to display individual items. This is my code, i hope its understandable. I'm using django 1.11

1.my urls page:

urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^about/', views.aboutus, name='aboutus'),
url(r'^specialoffers/', views.offers, name='offers'),
url(r'^contactus/', views.contact, name='contact'),
url(r'^excursions/', views.excursions, name='excursions'),
url(r'^singleexcursion/<int:id>/', views.singleexcur, name='singleexcur'),
url(r'^booking/', views.booking, name='booking'),
url(r'^confirmation/', views.confirming, name='confirming'),
url(r'^payment/', views.payment, name='payment'),

]

  1. My views page:

    def singleexcur (request,id):

    excur = Excursion.objects.get(id = id)
    
    
    return render(request, "excursions/single.html", {"excur": excur })
    
  2. my error: TypeError at /singleexcursion// singleexcur() missing 1 required positional argument: 'id'



Solution 1:[1]

An easy way of handling an empty id is to have multiple url for excursion:

 url(r'^singleexcursion/', views.singleexcur, name='singleexcur'),
 url(r'^singleexcursion/<int:id>/', views.singleexcur, name='singleexcur'),

Solution 2:[2]

Since , a new syntax was introduced, such that you can write parameters like <int:id>, but this is not applicable in url(..) constructs, here you need to write the path in a regex-like syntax, so you can specify the URL as:

# before Django-2.0

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^about/$', views.aboutus, name='aboutus'),
    url(r'^specialoffers/$', views.offers, name='offers'),
    url(r'^contactus/$', views.contact, name='contact'),
    url(r'^excursions/$', views.excursions, name='excursions'),
    url(r'^singleexcursion/(?P<id>\d+)/', views.singleexcur, name='singleexcur'),
    url(r'^booking/$', views.booking, name='booking'),
    url(r'^confirmation/$', views.confirming, name='confirming'),
    url(r'^payment/$', views.payment, name='payment'),
]

Or in and higher, you can use path(..) [Django-doc]. You can interleave it with re_path(..) [Django-doc], which is the new name of url(..) since url(..) [Django-doc] will probably eventually get removed.

For example:

# Django-2.0 and above

urlpatterns = [
    re_path(r'^$', views.home, name='home'),
    re_path(r'^about/$', views.aboutus, name='aboutus'),
    re_path(r'^specialoffers/$', views.offers, name='offers'),
    re_path(r'^contactus/$', views.contact, name='contact'),
    re_path(r'^excursions/$', views.excursions, name='excursions'),
    path('singleexcursion/<int:id>/', views.singleexcur, name='singleexcur'),
    re_path(r'^booking/$', views.booking, name='booking'),
    re_path(r'^confirmation/$', views.confirming, name='confirming'),
    re_path(r'^payment/$', views.payment, name='payment'),
]

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 Guillermo Quiros
Solution 2