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

查看:172
本文介绍了如何通过 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(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 -

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.无需为一个按钮创建整个表单.完全删除您的表单,哪个结束标记也是错误的(阅读 Brandon 的评论).

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

在您的 views.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>

仅此而已.没有标题,没有正文,什么都没有.此代码将在 index.html 中按 AJAX 动态插入.

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})

在这种情况下,您将不需要视图输出和 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天全站免登陆