Python 制作一个运行.py的按钮

Python 制作一个运行.py的按钮,python,html,django,Python,Html,Django,我正在制作一个网站,将采取2个输入文件,并返回这些文件之间的差异作为输出。我已经用python编写了textcompare程序,前两个网页将接收上传的文件并将其与textcompare.py一起存储在一个文件夹中。我遇到问题的最后一个网页只有一个按钮,上面写着“执行”。这个按钮背后的主要思想是,当我点击它时,它将运行textcompare.py,程序生成一个输出,然后它将发送给用户下载。问题是,我不知道如何定义一个运行textcompare.py的类。我是python和django的新手,因此感

我正在制作一个网站,将采取2个输入文件,并返回这些文件之间的差异作为输出。我已经用python编写了textcompare程序,前两个网页将接收上传的文件并将其与textcompare.py一起存储在一个文件夹中。我遇到问题的最后一个网页只有一个按钮,上面写着“执行”。这个按钮背后的主要思想是,当我点击它时,它将运行textcompare.py,程序生成一个输出,然后它将发送给用户下载。问题是,我不知道如何定义一个运行textcompare.py的类。我是python和django的新手,因此感谢您对我的支持

在execute.html中,我有

{% extends 'base.html' %}

{% load static %}

{% block content %}

    <div>
      <form action= '{% url my_view_name%}' method="POST">
          <input type="submit" value="Execute" id="Execute" />
      </form>
    </div>


  {% if uploaded_file_url %}

  {% endif %}


{% endblock %}
还有url.py

from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

from uploads.core import views


urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^uploads/uploadOne/$', views.uploadOne, name='uploadOne'),
    url(r'^uploads/uploadTwo/$', views.uploadTwo, name='uploadTwo'),
    url(r'^uploads/execute/$', views.execute, name='execute'),
    url(r'^admin/', admin.site.urls),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是textcompare.py

# this program is to compare 2 files and extract the differences in a new file
# the line old.csv, new.csv, and update.csv can be replaced to another type of file such as .txt
with open('file1.csv', 'r') as t1, open('file2.csv', 'r') as t2: # here i open 2 files and make python read it
    fileone = t1.readlines() # read lines from 1st file
    filetwo = t2.readlines() # read lines from 2nd file

with open('difference.csv', 'w') as outFile: # create output file called with update.csv
    for line in filetwo: #for statement
        if line not in fileone: # if line is not same
            outFile.write(line) # print the output

我相信url.py很好,但是views.py和execute.html还没有准备好。如果有人能帮我定义这个类并浏览execute.html,那就太棒了。提前谢谢

好的,它不是那样工作的。您正在告诉您的表单以我的名字进行uri查看。在url.py中使用此名称创建路由。定义在该url上调用的函数。您的url模式如下所示

from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

from uploads.core import views


urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^uploads/uploadOne/$', views.uploadOne, name='uploadOne'),
    url(r'^uploads/uploadTwo/$', views.uploadTwo, name='uploadTwo'),
    url(r'^uploads/execute/$', views.execute, name='execute'),
    url(r'^my_view_name/$', views.my_view_name, name='my_view_name'),
    url(r'^admin/', admin.site.urls),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
在您的视图中.py PS:这绝对不是做到这一点的方法,理想情况下,它应该是一起上传文件,然后立即计算差异,但由于我不知道您的应用程序正在做什么,所以我不会进入这一步

def yourscript():
    # You would need to give proper path for opening file on server, this might not work as it is
    with open('file1.csv', 'r') as t1, open('file2.csv', 'r') as t2: # here i open 2 files and make python read it
        fileone = t1.readlines() # read lines from 1st file
        filetwo = t2.readlines() # read lines from 2nd file

    with open('difference.csv', 'w') as outFile: # create 
    output file called with update.csv
        return_string = ''
        for line in filetwo: #for statement
            if line not in fileone: # if line is not same
                return_string+=line # print the output
        return return_string

我用你的建议更新了qs,关于def my__view_name(request),我真的不明白你把脚本内容放在那里是什么意思,上面所做的是什么,这就是你的意思吗?所以在你的脚本中,你把输出放在了另一个文件中。如果你想这么做,那完全是另一回事了。下面是您可以执行的操作,而不是outFile.write(line)-->print(line)它将在终端(命令提示符)中逐行打印差异。现在,若你们想把整个东西编译成一个文件或者发送到浏览器,那个就由你们自己决定了。但要确保终端打印正常。我大致更新了答案。不要期望工作是这样的。这只是一个蓝图。这就是问题所在,它必须以.csv/.txt格式生成一个输出,然后在windows server 2012中发布整个内容,使每个人都可以在我的环境中使用它。您编写的代码,从return_string=''一直到结尾,它是否生成输出或在终端中打开?它会将内容放入终端,但您可以从那里构建代码。若这个东西按预期在终端中写入,那个么剩下的就是如何将它放入文件中。这是另一个问题/
def my_view_name(request):
    #your script contents will go here. not literally but this is where gist of script should run
    # calculate the difference and put it in context var and return as follows
    diff = yourscript()
    context['diff'] = diff
    return render(request, 'core/uploadOne.html', context)
def yourscript():
    # You would need to give proper path for opening file on server, this might not work as it is
    with open('file1.csv', 'r') as t1, open('file2.csv', 'r') as t2: # here i open 2 files and make python read it
        fileone = t1.readlines() # read lines from 1st file
        filetwo = t2.readlines() # read lines from 2nd file

    with open('difference.csv', 'w') as outFile: # create 
    output file called with update.csv
        return_string = ''
        for line in filetwo: #for statement
            if line not in fileone: # if line is not same
                return_string+=line # print the output
        return return_string