简体   繁体   English

如何将网址与django urlconf匹配?

[英]how to match an url with django urlconf?

Im implementing a payment module called mercadopago (like paypal) and I need to handle a http request that is send to my site when someone make a payment(IPN) with information in in an url of this type: 我正在实施一个名为mercadopago的支付模块(如paypal),我需要处理一个http请求,当有人通过此类网址中的信息进行付款(IPN)时,该请求会发送到我的网站:

POST /notifications?topic=payment&id=identificador-de-notificacion-de-pago POST / notifications?topic = payment&id = identificador-de-notificacion-de-pago

(mercadopago send a POST request to my site every time a payment impact) (mercadopago每次付款时都会向我的网站发送POST请求)

but i cant make it match with the django url system. 但我不能使它与django网址系统匹配。 i tried the following url: 我尝试了以下网址:

    url(r'^notifications$', 'views.notifications', name='notifications'),

I tried with diferent combination and consulted the apache log file and threw me error 500 我尝试了不同的组合,并咨询了apache日志文件,并告诉我错误500

The view that handdle the url is the following: 移动网址的视图如下:

    @csrf_exempt
    def IpnProxy(request, **kwargs):
        mp = mercadopago.MP("*********", "*********") 
        paymentInfo = mp.get_payment_info(kwargs["id"])
        if paymentInfo["status"] == 200:
            return paymentInfo["response"]
        else:
            return None

I dont know if have to configure singals or something else. 我不知道是否必须配置singals或其他东西。

Maybe im getting it wrong but mercadopago make the post request to my server, I cant change that. 也许即时通讯错误,但mercadopago向我的服务器发出邮件请求,我无法改变它。 Here is their documentation http://developers.mercadopago.com/documentation/instant-payment-notifications?lang=en_US And here is their example project in python: https://github.com/mercadopago/sdk-python/blob/master/examples/instant-payment-notifications/receive-ipn.py 这是他们的文档http://developers.mercadopago.com/documentation/instant-payment-notifications?lang=en_US这里是他们在python中的示例项目: https//github.com/mercadopago/sdk-python/blob/主/例子/即时付款通知/ receive-ipn.py

Everything after the ? 之后的一切? mark is a Query string with request GET parameters. mark是一个带有请求GET参数的Query字符串 They are not included into the url patterns in urls.py : 它们不包含在urls.py的url模式中:

url(r'^notifications/$', view_notifications)

Get of the GET parameters from request.GET QueryDict in the view: 从视图中的request.GET QueryDict获取GET参数:

def view_notifications(request):
    topic = request.GET.get('topic')
    print topic

See also: 也可以看看:

You are encoding the parameters incorrectly for a POST in Django. 您正在为Django中的POST错误地编码参数。 POST requests get their parameters encoded in the request body. POST请求将其参数编码在请求正文中。 Please take a look at the Request and response objects documentation for Django. 请查看Django的请求和响应对象文档。 If you really want to use the URL you provided you will need to use a GET instead of a POST. 如果您确实想使用您提供的URL,则需要使用GET而不是POST。

--- EDIT --- ---编辑---

Give the following a try: In your urls.py file after you've imported notification. 尝试以下操作:在导入通知后的urls.py文件中。

url(r'^notification$', notification, name='notification')

In your settings.py: 在您的settings.py中:

APPEND_SLASH=False

You will almost certainly need to also turn of CSRF protection since you won't have a CSRF cookie. 您几乎肯定需要转换CSRF保护,因为您没有CSRF cookie。

You will be able to retrieve the parameters as follows: 您将能够按如下方式检索参数:

def notification(request, **kwargs):
    body = "topic = '{0}'\nid = '{1}'\n".format(request.GET.get('topic'), request.GET.get('id'))
    response = HttpResponse(body, content_type="text/plain")
    return response

Testing then gives us: 测试然后给我们:

hackworth:~$ curl -X POST "http://127.0.0.1:8000/notification?topic=payment&id=identificador-de-notificacion-de-pago"
topic = 'payment'
id = 'identificador-de-notificacion-de-pago'

All of the above is a bad idea since POST requests generally have a body, and Django really does not expect query parameters on a POST url. 以上所有都是一个坏主意,因为POST请求通常有一个正文,而Django实际上并不期望POST url上的查询参数。 Also, there is now a security vulnerability since CSRF protection had to be turned off to make this work. 此外,现在存在一个安全漏洞,因为必须关闭CSRF保护才能使其工作。

The way the documentation reads, you will receive a URL from mercadopago that you retrive with a get request. 文档的读取方式,您将收到来自mercadopago的URL,您可以使用get请求进行检索。 It is not clear that that they will send you a POST request with query parameters. 目前尚不清楚他们是否会向您发送带有查询参数的POST请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django URLconf当前路径不匹配 - django URLconf current path didn't match 使用定义的 URLconf,Django 尝试了这些 URL 模式 - Using the URLconf defined,Django tried these URL patterns 使用在 Lecture3.urls 中定义的 URLconf,Django 尝试了这些 URL 模式,顺序如下: admin/ 当前路径,hello/,不匹配任何一个 - Using the URLconf defined in lecture3.urls, Django tried these URL patterns, in this order: admin/ The current path, hello/, didn’t match any of thes 使用 lec3.urls 中定义的 URLconf,Django 尝试了这些 URL 模式,顺序如下: admin/ hello/ 空路径与其中任何一个都不匹配 - Using the URLconf defined in lec3.urls, Django tried these URL patterns, in this order: admin/ hello/ The empty path didn't match any of these Django:匹配urlconf中一组关键字正则表达式中的一个或多个 - Django: match one or more from a set of keyworded regular expressions in an urlconf 使用十五.urls 中定义的 URLconf,Django 尝试了这些 URL 模式 - Using the URLconf defined in fifteen.urls, Django tried these URL patterns Django URLconf:重定向,同时保留URL的查询部分和unicode - Django URLconf: redirect while preserving URL's query part and unicode 使用名称中定义的 URLconf,Django 按此顺序尝试了这些 URL 模式 - Using the URLconf defined in name, Django tried these URL patterns, in this order 使用 Django 中定义的 URLconf 按以下顺序尝试了这些 URL 模式: - Using the URLconf defined in Django tried these URL patterns in this order: Webroot上的Django urlconf 404 - django urlconf 404 on webroot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM