如何在Django中创建一个用于将用户重定向到输入的地址的表单?

huangapple go评论91阅读模式
英文:

How can make a form for redirecting user to the entered address in Django?

问题

我有点困惑,不知道应该如何制作一个简单的表单,该表单接受用户输入的网站 URL,并在点击提交按钮后进行重定向。我应该使用UserCreationForm吗?如果是这样,我应该使用哪些字段?还是应该创建一个自定义表单?

英文:

I am a little confused as to how should I be working on making a simple form that takes a website url input from a user and redirects on clicking the submit form button. Should I be using the UserCreationForm , if so , what fields should I be using for the same. Or should I be creating a custom form for the same .

答案1

得分: 1

以下是翻译好的部分:

模板:

<form action="{% url 'my_view' %}" method="POST">
  {% csrf_token %}
  <input name="url" type="url">
  <input type="submit">
</form>

视图:

@login_required
def my_view(request):
    if request.method == "POST":
        return redirect(request.POST.get("url"))
    else:
        return render(request, "my_template.html")
英文:

Here is a (simplified) example:

Template:

&lt;form action=&quot;{% url &#39;my_view&#39; %}&quot; method=&quot;POST&quot;&gt;
  {% csrf_token %}
  &lt;input name=&quot;url&quot; type=&quot;url&quot;&gt;
  &lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;

View:

@login_required
def my_view(request):
    if request.method == &quot;POST&quot;:
        return redirect(request.POST.get(&quot;url&quot;))
    else:
        return render(request, &quot;my_template.html&quot;)

答案2

得分: 0

如果您打算使用文本字段,那么应避免使用表单。创建一个普通的HTML表单,其中包含一个文本输入字段,然后在后端获取该值。

HTML:mytemplate.html

<form method="post">
{% csrf_token %}
<input type="url" name="url" placeholder="https://example.com" />
<input type="submit" value="提交" />
</form>

views.py

from django.shortcuts import redirect, render

def redirect_to_url(request):
    if request.method == "POST":
        url = request.POST.get('url')
        return redirect(url)
    else:
        return render(request, 'mytemplate.html', {})

这是一个非常基本的实现。您还可以验证URL是否存在。

import requests
from django.shortcuts import redirect, render
from django.http import HttpResponse

def redirect_to_url(request):
    if request.method == "POST":
        url = request.POST.get('url')
        resp = requests.get(url)
        if resp.status_code == 200:    # 其他验证和条件
            return redirect(url)
        return HttpResponse('无效的URL')
    else:
        return render(request, 'mytemplate.html', {})

了解更多关于input type url和Django redirect的信息。

英文:

If you are going to use a text field then you should avoid forms. Create a normal HTML form with a input type text and get that value in backend.

HTML: mytemplate.html

&lt;form method=&quot;post&quot;&gt;
{% csrf_token %}
&lt;input type=&quot;url&quot; name=&quot;url&quot; placeholder=&quot;https://example.com&quot; /&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;

views.py

from django.shortcuts import redirect, render


def redirect_to_url(request):
    if request.method == &quot;POST&quot;:
        url = request.POST.get(&#39;url&#39;)
        return redirect(url)
    else:
        return render(request, &#39;mytemplate.html&#39;, {})

This is a very basic implementation. You can also verify if the url exists.

import requests
from django.shortcuts import redirect, render
from django.http import HttpResponse


def redirect_to_url(request):
    if request.method == &quot;POST&quot;:
        url = request.POST.get(&#39;url&#39;)
        resp = requests.get(url)
        if resp.status_code == 200:    # some other validation and conditions
            return redirect(url)
        return HttpResponse(&#39;Invalid URL&#39;)
    else:
        return render(request, &#39;mytemplate.html&#39;, {})

For more about input type url and Django redirect.

huangapple
  • 本文由 发表于 2020年1月3日 13:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定