【问题标题】:django views.py to call another python scriptdjango views.py 调用另一个 python 脚本
【发布时间】: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


【解决方案1】:

无论你想在浏览器中显示什么字符串,在 HttpResponse 中返回它。

你可以试试这样的:

#validation.py
def validate(url1, url2):
    # perform validation here
    if valid:
        return "urls are valid"
    else:
        return "urls are invalid"


#views.py
def shortcuts_process(request):
    print request.POST
    url1 = request.POST.get('url1', '')
    url2 = request.POST.get('url2'. '')
    return HttpResponse(validate(url1, url2))

希望对您有所帮助。
表单验证请参考django书籍第7章(http://www.djangobook.com/en/2.0/chapter07.html)

【讨论】:

  • 感谢 krthkr。但是如何实现以下。假设validate() 有两个打印语句。您如何将两个打印件放在浏览器中?
  • 将数据发送到模板。 datadict = {'statement1':'wheels of fortune', 'statement2':'all hail brassica prime'} render_to_response('template.html', datadict)
  • @limelights 如果我的消息在 view.py 中,这会有所帮助,以便我可以创建模板。在这里,我在我的模块中收到了消息。说validation.pyprint "Validation in progress" .. .. print "URLs are valid" 这个怎么加到模板里。
  • 您可以将邮件从您的validation.py 发送到您的views.pyfrom validation import validator,然后是 message = validator(url1, url2),最后是 datadict = {....}
  • 听起来不错。让我试试然后回来.. :) 是否无法在用户浏览器中流式传输这些输出。通过这种模板方式,我们获取所有输出,将其放入 html 中,然后显示。所以我们错过了实时输出不是吗。如果我的理解有误,请纠正我。
【解决方案2】:

谢谢大家.. 我设法得到了答案。 而是让我的函数 validate_urlprint 的值,将其更改为 return 给了我想要的结果。

我太笨了才发现这个。失败是成功的垫脚石!! :O)

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多