Py学习  »  Django

Django 3.0中未定义名称“render_to_response”

VEERA BABU • 2 年前 • 422 次点击  

在将我的项目从Django 1.8更新到Django 3.0之后,我面临着这样一个问题:“需要2个值才能在for循环中解包;得到1”

让我们考虑一下我的观点。py as:

def add_items(request, pk, ot):
    client = request.user.client
    val1 = []
    warehouse = [
        (str(use.pk), use.name)
        for use in WareHouse.objects.filter(client_id=client).
        exclude(is_active=False)]

    project = Project.objects.filter(client=request.user.client).exclude(is_deleted=True)
    data1 = OtherOrder.objects.filter(id=pk)

    if data1.exists() and (len(warehouse) == 1):
        order = data1[0]
        if order.warehouse is None:
            try:
                order.warehouse_id = warehouse[0][0]
                order.save()
            except BaseException as e:
                logger.exception(e)

    data = OtherOrderItem.objects.filter(other_order_id=pk)
    total = data.aggregate(Sum('total_cost')).get('total_cost__sum') or 0.00
    charges = OtherOrderAdditionalCharges.objects.filter(order_id=pk)
    if charges.filter(charges_calculated=False):
        flag = True

    val = [i.id for i in data]
    for j in val:
        data3 = Movements.objects.values('other_order_item','damaged_quantity').filter(other_order_item=j, other_order_id=pk).annotate(Sum('quantity'))
        val1.append(data3)
    back = '/stock/other_orders/'
    return render(request,'stock/add_items.html',locals())

我该如何解决我的问题

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133231
 
422 次点击  
文章 [ 2 ]  |  最新文章 2 年前
Ankit Tiwari
Reply   •   1 楼
Ankit Tiwari    2 年前

render_to_response() 在Django 3.0使用中被删除 render() 最后你的代码会是这样的

class StockPricingView(View):
    def get(self, request, pk):
        data1 = OtherOrder.objects.get(id=pk)
        data = PricingModule.objects.filter(item__other_order_id=pk)
        charges = OtherOrderAdditionalCharges.objects.filter(order_id=pk)
        if charges.exists():
            total_charges = charges.aggregate(Sum('amount')).get('amount__sum') or 0.00
            order_total = data1.otherorderitem_set.all().aggregate(Sum('total_cost')).get('total_cost__sum') or 0.00
            per_charges = (total_charges/order_total)*100
        return render(request, 'stock/otherorders/stock_pricing_view.html')

render() 函数接受中指定的多个参数 doc.

lucutzu33
Reply   •   2 楼
lucutzu33    2 年前

来自文档:

render_to_response(template_name, context=None, content_type=None, status=None, using=None)

Deprecated since version 2.0.

This function preceded the introduction of render() and works similarly except that it doesn’t make the request available in the response.

https://docs.djangoproject.com/en/2.2/topics/http/shortcuts/#render