英文:
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:
<form action="{% url 'my_view' %}" method="POST">
{% csrf_token %}
<input name="url" type="url">
<input type="submit">
</form>
View:
@login_required
def my_view(request):
if request.method == "POST":
return redirect(request.POST.get("url"))
else:
return render(request, "my_template.html")
答案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
<form method="post">
{% csrf_token %}
<input type="url" name="url" placeholder="https://example.com" />
<input type="submit" value="Submit" />
</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', {})
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 == "POST":
url = request.POST.get('url')
resp = requests.get(url)
if resp.status_code == 200: # some other validation and conditions
return redirect(url)
return HttpResponse('Invalid URL')
else:
return render(request, 'mytemplate.html', {})
For more about input type url and Django redirect.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论