n.l.
n.l.

Reputation: 21

Python Django multiple request parameters

I am new to python & django and am trying to build a simple web application. I am running into the below issue

I am passing the following code

def view(request, cases_id, transactions_id):
    item = Cases.objects.get(pk=cases_id)
    item2 = Transactions.objects.get(pk=transactions_id)
    return render(request, 'view.html', {'item': item, 'item2': item2})

and getting the following error:

view() missing 1 required positional argument: 'transactions_id'

My urls.py:

from django.urls import path 
from . import views 

urlpatterns = [
    path('', views.home, name='home'), 
    path('new', views.new, name='new'),
    path('edit', views.edit, name='edit'),
    path('view/<cases_id>',views.view, name='view'),
]

Upvotes: 0

Views: 85

Answers (1)

John McCabe
John McCabe

Reputation: 712

I think Marat has the solution in the comments above. Just update your path to include the transaction_id variable.

from django.urls import path 
from . import views 
urlpatterns = [
    path('view/<cases_id>/<transaction_id>',views.view, name='view'),
    #                     ^^^^ Add this bit here
]

You could also handle this particular error by adding a default value to your view method.

def view(request, cases_id, transactions_id=0):
    ...

This way, if no transaction_id is present in the URL, the method will have a default value to use. You can replace 0 with whatever value makes the most sense for your application.

Upvotes: 1

Related Questions