首页 > 解决方案 > 如何获取用户输入并将其添加到 url 并在新选项卡中打开 url?

问题描述

我对此一无所知。我需要做一个简单的网站。

我将接受用户的输入。假设输入是'LOL'然后我需要用这个网址打开一个新标签 - “https://www.help.me/LOL”

#请帮帮我

标签: javascripthtmlweb

解决方案


您可以使用 django 表单在您的项目目录中的 views.py 文件中,您可以使用此代码

from django import forms
from django.http import HttpResponseRedirect

    
class inputform(forms.Form):
    inputfield = forms.CharField(label="Input")

def index(request):
    if request.method == "POST":
        x = inputform(request.POST)
        if x.is_valid():
            y = x.cleaned_data["inputfield"]
            return 
            HttpResponseRedirect(f"https://www.help.me/{y}")
    return render(request, "index.html", { "form": 
        inputform() } 

在 HTML 中,您可以编写以下代码来表示 HTML 页面中的输入字段

在 index.html 中;

<!DOCTYPE html>
<html lang="en">
  <head>
    <title> xyz </title>
  </head>
  <body>
    <form method="POST">
      {% csrf_token %}
      {{form}}
      <input type="submit">
    </form>
  </body>
</html>   

这样你就会得到你想要的结果


推荐阅读