Python 在Django中使用表单输入重定向到url的正确方法

Python 在Django中使用表单输入重定向到url的正确方法,python,django,Python,Django,我设法从HTML表单到URL模式获取了值,但这并不是解决问题的优雅方法。所以我在问,既然我又面临同样的问题,那最好的办法是什么呢 这是我所拥有的东西的简化版本: 我的HTML表单: <form method='post' action={% url 'pressed_button' %}> <button name="choice" value="Correct-Button"> Click Me </button> <button name="

我设法从HTML表单到URL模式获取了值,但这并不是解决问题的优雅方法。所以我在问,既然我又面临同样的问题,那最好的办法是什么呢

这是我所拥有的东西的简化版本:

我的HTML表单

<form method='post' action={% url 'pressed_button' %}>
  <button name="choice" value="Correct-Button"> Click Me </button>
  <button name="choice" value="Wrong-Button"> Dont Click Me </button>
</form>
url(r'^PressedButton/$', views.PressedButtonView.as_view(), name='pressed_button'),
url(r'^PressedButton/(?P<choice>\w+)$', views.PressedButtonView.as_view(), name='pressed_button_with_user_choice'),
class PressedButtonView(View):
    def get(self, request, choice):
        return render(request, 'weirdapp/buttonclicked.html', {'button': choice})

    def post(self, request):
        choice = request.POST['choice']

        return redirect(reverse('pressed_button_with_user_choice', kwargs={'choice': choice}))

现在,我想如果我可以在表单的“action”属性中发送按下按钮的值

<form method='post' action={% url 'pressed_button_with_user_choice' 'pressedButtonValue' %}>

这将是一个更好的代码

另外,如果我能得到第二个相关问题的答案,那么
中的reverse方法返回重定向(reverse())
的作用是什么?为什么不直接重定向到URL模式名称


谢谢。

为什么不为每个按钮分别制作带有适当的
操作的表单呢?

我能推荐一种替代方法吗?也许在表单为
post
后验证正确/错误的选择会更好?在
表单\u valid
中,您可以根据正确/不正确的选择重定向到另一个页面


换句话说,如果你在HTML中添加提示,比如
class=Correct choice
,你可能会遇到一些作弊者:p

过了一会儿,我回来回答我自己的问题,因为我想到了一个好的解决方案

使用Javascript,我们可以使用从按下的按钮获得的值,将表单数据的POST请求发送到我们想要的url。以下是方法:

HTML:

<form id="my-form">
 Age: <input name="age" value="31"/>
</form>

<button class="form-submitter" data-choice="Correct-Button"> Click Me </button>
<button class="form-submitter" data-choice="Wrong-Button"> Dont Click Me </button>
(function(){
  $('.form-submitter').on('click', function(e){
    let formData = new FormData(document.getElementById('my-form'));
    let buttonChoice = $(this).data('choice');

    $.ajax({
      url: '/PressedButton/ + buttonChoice',
      data: formData,
      method: 'POST',
      processData: false,
      contentType: false,
      success: function(response){
        console.log("successful communication", response);
      },
    });

  });  
})();
url(r'^PressedButton/(?P<choice>\w+)$', views.PressedButtonView.as_view(), name='pressed_button'),
class PressedButtonView(View):
    def post(self, request, choice):
        #age = request.POST['age']
        return render(request, 'weirdapp/buttonclicked.html', {'button': choice})
url.py:

<form id="my-form">
 Age: <input name="age" value="31"/>
</form>

<button class="form-submitter" data-choice="Correct-Button"> Click Me </button>
<button class="form-submitter" data-choice="Wrong-Button"> Dont Click Me </button>
(function(){
  $('.form-submitter').on('click', function(e){
    let formData = new FormData(document.getElementById('my-form'));
    let buttonChoice = $(this).data('choice');

    $.ajax({
      url: '/PressedButton/ + buttonChoice',
      data: formData,
      method: 'POST',
      processData: false,
      contentType: false,
      success: function(response){
        console.log("successful communication", response);
      },
    });

  });  
})();
url(r'^PressedButton/(?P<choice>\w+)$', views.PressedButtonView.as_view(), name='pressed_button'),
class PressedButtonView(View):
    def post(self, request, choice):
        #age = request.POST['age']
        return render(request, 'weirdapp/buttonclicked.html', {'button': choice})
下面是一个更具文档化的javascript工作模式。
.

这是否意味着我不需要两种URL模式?而且,它仍然感觉不到“pythonic”。这消除了重定向的需要。这是“web”部分存在的问题。不是在“python”部分。在没有中间视图的情况下,就没有任何东西是“pythonic”或“pythonic”了。
reverse
方法返回一个url。您可以将其与返回HttpResponseRedirect(反向(…)
一起使用。但是,您不需要将
反向
重定向
快捷方式一起使用。您可以简化
重定向(反向('pressed_button_with_user_choice',kwargs={'choice':choice}))
返回重定向('pressed_button_with_user_choice',choice=choice)
@Alasdair哦,我明白了。非常感谢。我对所有这些东西还是新手,所以我只想确保我正确理解您。我应该在视图中的一个函数中检查表单的POST请求内容,该函数在表单被
POST
ed时被调用。然后该函数重定向到另一个URL。对吗?不,你不应该直接进入邮局,