【问题标题】:How to link input in html form to its form actions in django?如何将html表单中的输入链接到django中的表单操作?
【发布时间】:2021-08-11 18:51:52
【问题描述】:

是否可以从 HTML 表单输入中获取值并在表单操作中使用它?

<form action="/wiki/ /" method="post">
       {% csrf_token %}
      <input class="search" type="text" name="q" placeholder="Search Encyclopedia">
</form>

在这里,我希望表单操作是“/wiki/input_in_q/”。你是怎么做到的?

【问题讨论】:

    标签: html django django-views django-templates


    【解决方案1】:

    #模板

    <form action="/wiki/input_in_q/ " method="post">
           {% csrf_token %}
           {{form.as_p}}
          <input class="search" type="text" name="q" placeholder="Search Encyclopedia">
    </form>
    
    

    Action 会将您发送到该网址:/wiki/input_in_q/。我认为您需要查看该网址。

    Views.py

    from .forms import YourForm
    
    def yourview(request):
       form = YourForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            value = form.save()
            print(value.(YourFormfeature))
        
        return render(request, 'first.html',context)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2020-07-21
      • 2020-10-26
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多