0

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'),
]
5
  • 3
    Please share your urls.py. Commented Aug 20, 2019 at 14:56
  • 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'), ] Commented Aug 20, 2019 at 15:13
  • So, what is unclear about the error? Your URL only sends cases_id. Where is the value for transactions_id supposed to be coming from? Commented Aug 20, 2019 at 15:22
  • sorry I am new to this. How do I pass both values in the url section? Commented Aug 20, 2019 at 15:29
  • 1
    path('view/<cases_id>/<transaction_id>',views.view, name='view'), Commented Aug 20, 2019 at 15:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.