samedi 19 décembre 2020

Conditional Execution in Same View

I want my declarations to be user-specific. I chose to do this by making my variable declarations in the view conditional based on user-type. Here's a sample.

def order_detail(request, order_id):
    order = get_object_or_404(Order, id=order_id)

    if request.user.is_supervisor:
        user_location = request.user.supervisor.town
    deliverer = User.objects.filter(deliverer__town=user_location)

    context={
        'order': order,
        'all_delivery_guys_in_town': deliverer
        }
    template = 'orders/order_mgt/detail.html'
    return render(request, template, context)

For context, I need supervisors to be able to view a list of available delivery guys within the order detail page.

In the code sample, I'm getting an error:

local variable 'user_location' referenced before assignment

1. How can I correct this error?

2. Is there a better way of making conditional declarations within the same view?

Aucun commentaire:

Enregistrer un commentaire