方法反向django错误

时间:2013-10-17 22:58:13

标签: python django reverse

我正在使用反向调用方法,但我有一些问题,我无法理解。 我的错误: 反向'shopping.views.payment_confirmation',参数'(35,)'和关键字参数'{}'找不到。

我的网址:

url(r'^payment_confirmation/(?P<id>\d+\d{2\})/$', 'payment_confirmation', name='payment_confirmation'),

我的观点:

def show(request):
    ...
    ...
    payment.submit(settings.SITE_URL + reverse("shopping.views.payment_confirmation", args=[payment.id]))

我的付款模式:

class Payment(models.Model):
    ...
    ...
    def submit(self, redirect_url):
        '''
            Sends self as a Payment through PagSeguro API.
            Payment instance MUST BE SAVED before calling this method.
        '''

        if not self.id:
            #Temporary to identify which problem caused the crash.
            raise ValidationError

        #creating a reference if its None
        if self.reference is None:
            self.reference = configs.PAGSEGURO_REFERENCE_PREFIX + str(self.id)

        document = Document()
        document.appendChild(self.to_element(document, redirect_url))

        response = document2dict(api.submit_payment(document))

        try:
            self.code = response['checkout']['code']
            self.answered_on = datetime.datetime.now()
            self.save()
        except:
            error_str = ""
            if type(response["errors"]["error"]) != list:
                response["errors"]["error"] = [response["errors"]["error"]]
            for error in response["errors"]["error"]:
                error_payment = ErrorPayment()
                error_payment.code = int(error['code'])
                error_payment.payment = self
                error_payment.save()
                error_str += "[%s: %s]" % (error_payment.code,
                                           error_payment.get_code_display())
            raise Exception(error_str)

错误在这里payment.submit(settings.SITE_URL + reverse(“shopping.views.payment_confirmation”,args = [payment.id])) 我',使用这个api https://bitbucket.org/leonardosantos/django-pagseguro2/

1 个答案:

答案 0 :(得分:2)

这一行:reverse(“shopping.views.payment_confirmation”,args = [payment.id])告诉Django在购物应用中的views.py文件中找到一个名为payment_confirmation的方法的url,该文件将接受付款ID参数。

在您共享的错误中,payment.id为35.错误是说您的shopping.views中没有名为payment_confirmation的方法,或者该方法不接受单个int作为参数。

您未在视图文件中共享payment_confirmation方法,因此似乎问题就在于此。你需要添加:

def payment_confirmation(payment_id):
 #do stuff

到您的观点。