我正在建立一个网站,该网站将接收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 %}
在views.py中,我有
from django.shortcuts import render, redirect
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from uploads.core.models import Document
from uploads.core.forms import DocumentForm
def home(request):
documents = Document.objects.all()
return render(request, 'core/home.html', { 'documents': documents })
def uploadOne(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
modified_name = "file1.csv".format()
filename = fs.save(modified_name, myfile)
#filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
return render(request, 'core/uploadTwo.html', {
'uploaded_file_url': uploaded_file_url
})
return render(request, 'core/uploadOne.html')
def uploadTwo(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
modified_name = "file2.csv".format()
filename = fs.save(modified_name, myfile)
#filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
return render(request, 'core/uploadTwo.html', {
'uploaded_file_url': uploaded_file_url
})
return render(request, 'core/uploadTwo.html')
def execute(request):
if request.method == 'POST':
import TextCompare.py
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 =
# 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
file1 = t1.readlines() # read lines from 1st file
file2 = 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()
context['diff'] = diff
return render(request, 'core/uploadOne.html', context)
我有urls.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
我认为urls.py很好,但是views.py和execute.html尚未准备好。如果有人可以帮助我定义该类并执行execute.html,那就太好了。预先感谢
答案 0 :(得分:0)
好的,它不能那样工作。您正在告诉您的表格使用uri my_view_name。在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'^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)
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)
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