如何修复django-oscar shipping UnboundLocalError?

问题描述 投票:1回答:1

我想向django-oscar添加送货方式,但即使我已经完成了文档页面上的所有内容,我也收到了UnboundLocalError错误。

Request Method: GET
Request URL:    http://127.0.0.1:8000/checkout/shipping-address/
Django Version: 2.1.7
Exception Type: UnboundLocalError
Exception Value:    
local variable 'methods' referenced before assignment

repository.朋友

from oscar.apps.shipping import repository
from . import methods

class Repository(repository.Repository):

    def get_available_shipping_methods(self, basket, user=None, shipping_addr=None, request=None, **kwargs):
        methods = (methods.Standard(),)
        if shipping_addr and shipping_addr.country.code == 'GB':
            # Express is only available in the UK
            methods = (methods.Standard(), methods.Express())
        return methods

methods.朋友

from oscar.apps.shipping import methods
from oscar.core import prices
from decimal import Decimal as D

class Standard(methods.Base):
    code = 'standard'
    name = 'Shipping (Standard)'

    def calculate(self, basket):
        return prices.Price(
            currency=basket.currency,
            excl_tax=D('5.00'), incl_tax=D('5.00'))

class Express(methods.Base):
    code = 'express'
    name = 'Shipping (Express)'

    def calculate(self, basket):
        return prices.Price(
            currency=basket.currency,
            excl_tax=D('4.00'), incl_tax=D('4.00'))
django django-oscar
1个回答
1
投票

我可以看到这是在文档中,但他们看起来有一个错误。

使用UnboundLocalError,您基本上是在查看范围问题。一个非常简单的例子是;

x = 10
def foo():
    x += 1
    print x
foo()

foo执行x时,foo无法使用。所以稍微改变进口以避免这种情况;

from oscar.apps.shipping import repository
from . import methods as shipping_methods

class Repository(repository.Repository):

    def get_available_shipping_methods(self, basket, user=None, shipping_addr=None, request=None, **kwargs):
        methods = (shipping_methods.Standard(),)
        if shipping_addr and shipping_addr.country.code == 'GB':
            # Express is only available in the UK
            methods = (shipping_methods.Standard(), shipping_methods.Express())
        return methods
© www.soinside.com 2019 - 2024. All rights reserved.