【问题标题】:Passing user input to external link in django将用户输入传递给 django 中的外部链接
【发布时间】:2021-02-03 02:04:08
【问题描述】:

我正在尝试创建一个简单的 django 站点,该站点将使用 requests.post 函数(我假设这是一个正确的方法,但也许有更好的方法)。

到目前为止,我已经使用 bootstrap 创建了一个简单的 .html 文件

<div class="form-group">
    <label for="exampleFormControlInput1">text_thing</label>
    <input type="text" class="form-control" placeholder="text_thing" name="text_thing">
</div>
<div class="form-group">
    <label for="exampleFormControlInput1">number_thing</label>
    <input type="number" class="form-control" placeholder="number_thing" name="number_thing">
</div>
<button type="submit" class="btn btn-secondary">Send data</button>
</form>

还有我的观点.py

from django.shortcuts import render, redirect
import requests

def my_view(requests):
    if requests.method == "POST":
        text_thing=  request.GET.get('text_thing')
        number_thing= request.GET.get('number_thing')
        post_data = {'text_thing', 'number_thing'}
        if text_thing is not None:
            response = requests.post('https://example.com', data=post_data)
            content = reponse.content
            return redirect('home')
        else:
            return redirect('home')
    else:
        return render(requests, 'my_view.html', {})

我希望站点执行以下操作:呈现自身允许用户在两​​个字段中输入内容,并在按下按钮后重定向到另一个站点 - 在本例中为 'home' 并将输入发送到 @ 987654325@。此时页面正确呈现,但按下按钮后没有任何反应,外部站点也没有收到任何数据。

【问题讨论】:

    标签: html django httprequest django-request


    【解决方案1】:

    您的代码中有几个错误需要先修复

    ...
    
    def my_view(requests):
        if requests.method == "POST":
            # Here is 1, use POST instead
            # text_thing=  request.GET.get('text_thing')
            # number_thing= request.GET.get('number_thing')
            # https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.POST
    
            text_thing = request.POST.get('text_thing', None)
            number_thing = request.POST.get('number_thing', None)
            # Here is 2, you are not creating a dict this way,
            # You are creating a set, put the variables with the keys.
            post_data = {'text_thing': text_thing, 'number_thing': number_thing}
            ....
    

    如果您的网站没有收到任何数据,请检查其他网站的响应, 以确保您调用的是正确的 API。

    希望对你有帮助

    【讨论】:

    • 我进行了更改,不幸的是,单击按钮后仍然没有任何反应,我认为即使链接了错误的 API,我也应该在单击“发送数据”按钮后重定向到 'home'
    • 打印您从该端点收到的响应,以了解实际情况。
    【解决方案2】:

    您的代码中有许多拼写错误。您将requestsrequest 混淆了。 您正在导入 requests 模块并将其传递给 my_view() 这是不正确的。

    my_view() 是一个视图函数,它采用 request 对象,而不是 requests 库。

    您还应该使用 request.POST 来获取有效负载。 request.GET 返回查询参数而不是有效负载。

    这是更新后的代码

    from django.shortcuts import render, redirect
    import requests
    
    def my_view(request):
        if request.method == "POST":
            text_thing=  request.POST.get('text_thing')
            number_thing= request.POST.get('number_thing')
            post_data = {'text_thing', 'number_thing'}
            if text_thing is not None:
                response = requests.post('https://example.com', data=post_data)
                content = reponse.content
                return redirect('home')
            else:
                return redirect('home')
        else:
            return render(request, 'my_view.html', {})
    

    【讨论】:

      【解决方案3】:

      感谢@Radwan Abu-Odeh 和@Underoos,我设法让它工作起来。非常感谢您的帮助。

      工作示例: .html代码

      <form method="POST">
      <div class="form-group">
          <label for="exampleFormControlInput1">text_thing</label>
          <input type="text" class="form-control" placeholder="text_thing" name="text_thing">
      </div>
      <div class="form-group">
          <label for="exampleFormControlInput1">number_thing</label>
          <input type="number" class="form-control" placeholder="number_thing" name="number_thing">
      </div>
      <button type="submit" class="btn btn-secondary">Send data</button>
      </form>
      
      

      views.py

      def my_view(request):
          if request.method == "POST":
              text_thing=  request.POST.get('text_thing', None)
              number_thing= request.POST.get('number_thing', None)
              post_data = {'text_thing': text_thing, 'number_thing': number_thing}
              if text_thing is not None:
                  response = requests.post('example.com', data=post_data)
                  return redirect('home')
              else:
                  return redirect('home')
          else:
              return render(request, 'my_view.html', {})
      

      【讨论】:

        猜你喜欢
        • 2017-01-19
        • 1970-01-01
        • 1970-01-01
        • 2019-12-08
        • 1970-01-01
        • 2014-08-14
        • 2021-07-24
        • 2019-01-04
        • 2020-09-05
        相关资源
        最近更新 更多