Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在HTML中将文件内容提交到Django视图_Python_Html_Django - Fatal编程技术网

Python 在HTML中将文件内容提交到Django视图

Python 在HTML中将文件内容提交到Django视图,python,html,django,Python,Html,Django,我正在使用Django v1.4(PythonV.2.7.3),并试图构建一个校对应用程序。“我的校对人”有一个“示例”页面,其中包含用户示例校对的链接列表,这些链接一个接一个地呈现在屏幕上。小样本如下所示: 这些示例文件保存在服务器上的MEDIA_根目录下,我想要的是一种方法,以便单击链接将POST消息中的文件内容传递到特定视图。该视图已经设计了一些代码,用于处理用户直接从其文件系统上传校对文件,因此我想做的基本上是使examples.html模板(如下所示)传递相同类型的信息,但服务器上已

我正在使用Django v1.4(PythonV.2.7.3),并试图构建一个校对应用程序。“我的校对人”有一个“示例”页面,其中包含用户示例校对的链接列表,这些链接一个接一个地呈现在屏幕上。小样本如下所示:

这些示例文件保存在服务器上的MEDIA_根目录下,我想要的是一种方法,以便单击链接将POST消息中的文件内容传递到特定视图。该视图已经设计了一些代码,用于处理用户直接从其文件系统上传校对文件,因此我想做的基本上是使examples.html模板(如下所示)传递相同类型的信息,但服务器上已存储的文件除外

examples.html的代码是:

我想我想要的是一种实现以下目标的方法:

{% for example in examples %}
    <form name="example" action="/server/proof" method="post">
    {% csrf_token %}
    <p>{{ example.exampleFile.name|cut:"./" }}</p>
    <input type="hidden" name="example" value="{{ example.exampleFile.name|cut:"./" }}">
    <input type="submit" value="Select">
    </form>
    <br>
{% endfor %}
  • 用户单击特定示例证据旁边的链接
  • 所有必要的信息都会加载到request.xml文件中
  • 这将作为POST证明请求(请求)发送到服务器
  • 证明(请求)将其视为常规上传文件,并读取文件内容
我的models.py文件如下所示:

from django.db import models

class Example(models.Model):
    exampleFile = models.FileField(upload_to='.')

如果需要的话,我很乐意提供更多信息。

我找到了一个解决方案,但我怀疑它有点不成熟

在examples.html中,我包括以下内容:

{% for example in examples %}
    <form name="example" action="/server/proof" method="post">
    {% csrf_token %}
    <p>{{ example.exampleFile.name|cut:"./" }}</p>
    <input type="hidden" name="example" value="{{ example.exampleFile.name|cut:"./" }}">
    <input type="submit" value="Select">
    </form>
    <br>
{% endfor %}
这是拼凑的,但它符合我的要求。希望以后有时间改进代码

{% for example in examples %}
    <form name="example" action="/server/proof" method="post">
    {% csrf_token %}
    <p>{{ example.exampleFile.name|cut:"./" }}</p>
    <input type="hidden" name="example" value="{{ example.exampleFile.name|cut:"./" }}">
    <input type="submit" value="Select">
    </form>
    <br>
{% endfor %}
def proof(request):
    if request.method == 'POST':
        print "DATA"
        print str(request.POST)
        try:
          defaultText = request.FILES['docfile'].read()
        except:
          examplePath = request.POST.get('example')
          with open(join(MEDIA_DIR, examplePath), 'r') as f:
            defaultText = f.read()

        proofText = ProofForm(request.POST)
    else:
        defaultText = ""
        proofText = ProofForm()
    c = {}
    c.update(csrf(request))
    c['form'] = proofText
    c['default_text'] = defaultText
    return render_to_response('server/proof.html', c)