【发布时间】:2018-07-10 12:26:40
【问题描述】:
我已经使用 oscar_fork_app 命令将我的运输应用程序分叉到一个名为 forked_apps 的文件夹中,并在 settings.py get_core_apps(['forked_apps.shipping']) 中添加,我只想创建提到的两种运输方式,标准和快递此链接中的文档:https://django-oscar.readthedocs.io/en/latest/howto/how_to_configure_shipping.html.
在 init.py 我有这个代码预先存在:
default_app_config = 'forked_apps.shipping.config.ShippingConfig'
在repository.py中我是这样写的:
from oscar.apps.shipping import repository
from .methods import *
class Repository(repository.Repository):
def get_available_shipping_methods(
self, basket, user=None, shipping_addr=None,
request=None, **kwargs):
methods = (Standard())
print("\n\nFetch availble shipping methods")
if shipping_addr:
# Express is only available in the UK
methods = (Standard(), Express())
return methods
在我写的methods.py中:
from decimal import Decimal as D
from oscar.apps.shipping import methods
from oscar.core import prices
class Standard(methods.FixedPrice):
code = 'standard'
name = 'Standard shipping'
charge_excl_tax = D('5.00')
class Express(methods.FixedPrice):
code = 'express'
name = 'Express shipping'
charge_excl_tax = D('10.00')
应该发生的是,shipping_methods.html 页面应该显示出来,但是在输入送货地址后,它会直接进入付款详情页面;这通常只有在没有定义运输方式的情况下才会发生,但是我在上面的代码中实现了两种运输方式,标准和快递。我不知道如何使它工作,即使打印语句不起作用。 还有其他我必须编写的附加代码吗?
如果你已经实现了,有人可以提供一些代码的解决方案吗?
【问题讨论】:
-
我看不出您发布的代码有什么问题。您确定分叉的应用程序实际上正在加载(您的分叉的
Repository类上的__init__()是否运行?)并且您正在测试的篮子需要运输(如果篮子中没有产品,则此代码不会运行)requires_shipping在他们的产品类上设置为 true)? -
我认为
methods = (Standard())应该是methods = (Standard(),),否则你返回的是错误的东西。在任何情况下,print语句都应该被执行,如果不是,那么正如 solarissmoke 指出的那样,还有其他一些问题。config.ShippingConfig代码是否完好无损? -
好的,所以我在我的shell中做了这个>>> from forked_apps.shipping.repository import Repository >>> r=Repository() ,这之后没有错误。这是检查是否的方法init() 工作正常吗?
-
不,这并不能告诉您 Oscar 是否正在加载您的课程。您需要使用
get_class()加载类,然后检查返回的是您的类而不是 Oscar 的默认值。 -
在 settings.py 中,你需要在分叉后用这个 'apps.shipping.apps.ShippingConfig' 替换 'oscar.apps.shipping.apps.ShippingConfig'
标签: python django python-3.x django-oscar