如何通过Django HTML按钮执行python代码? [英] How to execute python code by django html button?

查看:743
本文介绍了如何通过Django HTML按钮执行python代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过onclick执行我的python代码。运行服务器后,我得到结果。我的按钮不起作用。这是我的代码。

I want to execute my python code by onclick. I am getting result after running the server. My button is not working. Here is my code.

URL-

url(r'^index/$', index),

index.html-

index.html-

<html>
<body>
  <form action="/index/" method="GET">
    <input type="submit" value="Click">
  </form>
  {{output}}
</body>
</html>

views.py-

from django.shortcuts import render, render_to_response
from pythoncode import mycode
def index(request):
    if request.method=="GET":
        py_obj=mycode.test_code(10)
        py_obj.code()
        return render(request, 'index.html', {"output":py_obj.a})

我还创建了一个应用程序来分隔python代码-
应用程序名称为python代码,文件名为mycode

I created one more application to separate python code - application name is python code and file name mycode

class test_code:
    def __init__(self, a):
        self.a=a
        self.b=4
    def code(self):
        return self.a, self.b

请帮助我。我是Django的新手。预先感谢

please help me. I am new in Django. Thanks in advance

推荐答案

如果您只想单击并在页面上即时显示某些内容,则需要JavaScript和AJAX。无需只为一个按钮创建整个表单。完全删除您的表单,该结束标签也是错误的(请阅读布兰登的评论)。

If you just want to click and display something on the fly on your page, you'll need JavaScript and AJAX. There is no need to create whole form just for one button. Remove your form completely, which closing tag is also wrong (read Brandon's comments).

您可以在index.html中使用此代码段:

You can use this snippet in your index.html:

<button id="myClickButton" type="button">Click</button>
<div id="myOutput"></div>

现在让我们在单击按钮时触发一些操作:

Now let's trigger something when clicking on the button:

$("#myClickButton").click(function() {
    $.get("/output/", function(data) {
        $("#myOutput").html(data);
    }, "html");
});

上面的代码是jQuery。请阅读jQuery的官方文档。一切都解释了如何使用该库。

The above code is jQuery. Please read the official documentation of jQuery. There is everything explained how to use the library.

现在让我们进入您的views.py。

Now let's go to your views.py.

def index(request):
    return render(request, 'yourapp/index.html')

请记住将模板放在应用程序中的文件夹 templates 中。看起来应该像这样:

Remember to put your templates in a folder templates within your app. It should look like this:

--yourproject
|
|--yourapp
|----templates
|------yourapp
|--------index.html

在视图中显示其他视图。py:

Make another view in your views.py:

def output(request):
    if request.is_ajax():
        py_obj = mycode.test_code(10)
        return render(request, 'yourapp/output.html', {'output': py_obj.a})

您的output.html就像

Your output.html can be like this:

<p>{{ output }}</p>

仅此而已。没有头,没有身体,一无所有。

That's all. No header, no body, nothing. This code will be inserted per AJAX on the fly in index.html.

现在,让我们分析一下方法 code

Now let's analyze your method code:

def code(self):
    return self.a, self.b

您知道这里会发生什么吗?您只能在函数中返回一个值。您认为自己返回的a和b是整数。错误!此方法返回包含两个元素的元组。此方法将返回(10,4)
当您在索引视图中调用此方法时,它只会返回该元组,但您并未将其分配给变量,因此它会随风而行。这是没用的电话。

Do you know what happens here? You can return only ONE value in a function. You think you're returning a and b as integers. Wrong! This method returns a tuple with two elements. This method will return (10, 4). When you call this method in your index view it just returns this tuple, but you're not assigning it to a variable, so it will go with the wind. It's useless call.

我希望这可以使您了解如何操作。如果您不想使用JavaScript(和AJAX),则可以按POST发送表单,并在自己的视图中加以区分:

I hope this gives you an idea how you can do it. If you don't want to use JavaScript (and AJAX) you can send your form per POST and make a distinction in your view:

def index(request):
    if request.method == 'GET':
        return render(request, 'yourapp/index.html', {'output': ''})
    elif request.method == 'POST':
        py_obj = mycode.test_code(10)
        return render(request, 'yourapp/output.html', {'output': py_obj.a})

在这种情况下,您将不需要view output和output.html。您可以在表单中使用index.html。

In this case you won't need the view output and output.html. You can use your index.html with the form inside.

这篇关于如何通过Django HTML按钮执行python代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆