I am trying to get data from the database based on request with id u_id
and x_id
but I am not getting any data instead getting an error TypeError: display_data() missing 2 required positional arguments: 'u_id' and 'x_id'
can you help?
code
JavaScript
x
5
1
def display_data(request, u_id, x_id):
2
if request.method == 'POST':
3
widget['data'] = Widget.objects.get(u_id = u_id, x_id = x_id)
4
return widget
5
urls.py
JavaScript
1
2
1
url(r'^display_data/$', views.display_data, name="display_data"),
2
Advertisement
Answer
You need to get them from the POST data instead.
JavaScript
1
7
1
def display_data(request):
2
if request.method == 'POST':
3
u_id = request.POST.get('u_id')
4
x_id = request.POST.get('x_id')
5
widget['data'] = Widget.objects.get(u_id=u_id, x_id=x_id)
6
return widget
7
Also, you might want to use a form for validation.
If you want to get u_id
and x_id
as arguments like you are trying, you need to add them to the url.
JavaScript
1
2
1
url(r'^display_data/<int:u_id>/<int:x_id>', views.display_data, name="display_data"),
2
Edit:
Use filter
instead of get
if you are getting multiple objects
JavaScript
1
2
1
widget['data'] = Widget.objects.filter(u_id=u_id, x_id=x_id)
2