Skip to content
Advertisement

Type error when trying to fetch data Python Django

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

def display_data(request, u_id, x_id):
    if request.method == 'POST':
        widget['data'] = Widget.objects.get(u_id = u_id, x_id = x_id)
        return widget

urls.py

url(r'^display_data/$', views.display_data, name="display_data"),

Advertisement

Answer

You need to get them from the POST data instead.

def display_data(request):
    if request.method == 'POST':
        u_id = request.POST.get('u_id')
        x_id = request.POST.get('x_id')
        widget['data'] = Widget.objects.get(u_id=u_id, x_id=x_id)
        return widget

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.

url(r'^display_data/<int:u_id>/<int:x_id>', views.display_data, name="display_data"),

Edit: Use filter instead of get if you are getting multiple objects

widget['data'] = Widget.objects.filter(u_id=u_id, x_id=x_id)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement