lundi 31 décembre 2018

redirect in django does work in POST method

i have views in django that is called by $.post() method of jquery that is working fine. But when i redirect inside that post it's not redirecting me but outside its redirecting me. here is view called by jquery $.post()

@csrf_exempt
def order_detail(request):
    category_name = request.session.get('category_name')
    category = Category.objects.get(name=category_name)
    price = Price.objects.get(category=category)
    if request.session['industry_name']:
        industry_name = request.session.get('industry_name')
        industry = Industry.objects.get(name=industry_name)
        images = SampleImages.objects.filter(category=category, industry=industry)
        colors = SampleColor.objects.all()
    else:
        images = SampleImages.objects.filter(category=category)
        colors = SampleColor.objects.all()
    if request.method == 'POST':
        image_list = request.POST.getlist('idSelectedImages[]')
        color_list = request.POST.getlist('idSelectedColors[]')
        package_price = request.POST.get('packagePrice')
        package_name = request.POST.get('packageName')
        if image_list:
            for image_id in image_list:
                request.session['image_list']['{}'.format(image_id)] = image_id
                request.session.modified = True
        if color_list:
            for color_id in color_list:
                request.session['color_list']['{}'.format(color_id)] = color_id
                request.session.modified = True
        return redirect('order:order_brief')
    else:
        request.session['image_list'] = {}
        request.session.modified = True
        request.session['color_list'] = {}
        request.session.modified = True

    return render(request, 'order/order_detail.html', {'images': images, 'colors': colors, 'price': price})

and urls.py

app_name = 'order'
urlpatterns = [
    path('', views.index, name='index'),
    path('order/detail/', views.order_detail, name='order_detail'),
    path('order/brief/', views.order_brief, name='order_brief'),
]




Aucun commentaire:

Enregistrer un commentaire