【发布时间】:2013-06-10 01:33:35
【问题描述】:
我是 Django 新手。请帮我解决以下问题。
我有一个提供 URL1 和 URL2 的用户表单。这些 URL 需要传递给另一个 Python 脚本[redirects.py],它将执行验证以检查它们是否采用有效的 URL 格式并将消息返回给用户。
现在我的问题是如何写我的views.py 以完成这项工作。我知道我们可以在我的views.py 中导入redirects.py 并调用它。但我不知道如何在浏览器中打印消息。请帮忙。如果需要更多信息,请告诉我。
def shortcuts_process(request):
print request.POST
return HttpResponse("Source is %s\nDestination is %s" % (request.POST['source'],request.POST['destination']))
更新:
这是我的脚本概述。我的系统中有一个 python 脚本[redirects.py],它接受源 URL 和目标 URL。一旦被接受,它会验证它们是否为 URL 格式,然后进行备份,然后将它们添加到 .htaccess 并显示添加到文件中的行。在执行所有这些操作的同时,它会不断向用户提示脚本中发生的事情。
现在我已经制作了 django 来创建一个门户网站,该门户网站为用户提供输入源和目标。现在我想从views.py 调用脚本并在用户的网络浏览器中打印所有redirects.py 脚本输出。
请帮我解决这个问题,我花了一整天的时间寻找这个答案。谢谢。
Update 2:
请告诉我为什么以下内容没有显示在我的浏览器中
来自views.py
def shortcuts_process(request):
if 'source' in request.POST and 'destination' in request.POST and request.POST['source'] and request.POST['destination']:
src=request.POST['source']
desti= request.POST['destination']
my_result=process_input(src,desti)
return render_to_response('results.html',{'result': my_result}
来自results.html:
<html>
<head>
This is the result page to User
</head>
<body>
<ul>
{% for each_line in result %}
<p>{{ each_line }}</p>
{% endfor %}
</ul>
<p>
I'm supposed to be printed</p>
</body>
</html>
来自浏览器输出:
这是用户的结果页面
我应该被打印出来
从 Linux 提示符:
[2013 年 6 月 10 日 04:41:11]“GET /重定向 HTTP/1.1”200 727 [2013 年 6 月 10 日 04:41:14]“GET /choice?selection=shortcuts HTTP/1.1”200 817 URL 格式不正确 URL 格式不正确 [10/Jun/2013 04:41:18]“POST /shortcuts.conf HTTP/1.1”200 125
所以现在我的问题是,为什么消息 The URL is not in the right format 没有显示在浏览器上,而是显示在 Linux 提示符中。请帮忙。谢谢
【问题讨论】:
-
你应该阅读 django 表单验证
-
@karthikr 感谢您的回复。 django 表单在验证部分帮助我,但如果是别的。说,我有一个脚本来进行文件备份,使用提供的输入对文件进行更改,如何做到这一点。我想要的只是在用户浏览器中打印脚本输出
-
您可以在视图方法本身中执行此操作。获取表单、验证、处理并响应请求
-
我尝试了以下。
def shortcuts_process(request): if 'source' in request.POST and 'destination' in request.POST and request.POST['source'] and request.POST['destination']: result=take_backup() return render_to_response('result.html', {'result':result})但我在浏览器中看不到任何结果。相反,我可以在 Linux 提示符中看到信息[同时运行 manage.py runserver]
标签: python django django-views