需要在Django中上传的文件

时间:2014-05-09 12:06:28

标签: python django url upload

Django的初学者,所有的帮助将不胜感激。

我要做的是:

  • 用户以
  • 的形式上传pdf文件
  • 需要将pdf文件发送到SOAPpy服务器进行测试。

所以我的问题是如何找到上传的文件并将其发送到服务器。我希望代码能解释更多我正在尝试做的事情。我能够将pdf文件发送到媒体根目录。

models.py:

from django.db import models

class handle_UploadPdf(models.Model):
docfile = models.FileField(upload_to='.')

forms.py:

from django import forms

class UploadPdf(forms.Form):
docfile = forms.FileField(label='Browse')

class UploadPdfUrl(forms.Form):
docurl = forms.URLField(label='')

view.py

from django.shortcuts import render, render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.forms.formsets import formset_factory
from django.forms.models import modelformset_factory
from django.contrib.auth import authenticate, login
from django.contrib import messages
from datetime import datetime
import urllib, urllib2
import SOAPpy

from pdfchecker.models import handle_UploadPdf, timestamp1
from pdfchecker.forms import UploadPdf, UploadPdfUrl
from pdfchecker.server import connect
# This view consists of a formset containing the FileField for uploading PDF-files
# and also a URLField which contains the url to a file  
def upload(request):
    UploadPdfFormSet = formset_factory(UploadPdf)
    handleUploadPdfFormSet = modelformset_factory(handle_UploadPdf)
    if request.method == 'POST':
        formURL = UploadPdfUrl(request.POST)
        formsetFile = UploadPdfFormSet(request.POST, request.FILES)
        formsetHandle = handleUploadPdfFormSet(request.POST, request.FILES)
        if formURL.is_valid():
            formsetHandle.save()
            data = formURL.cleaned_data
            url_input = data['docurl']
            #timestamp = timestamp1.objects.all()
            result = connect(url_input) 
            context = {'result': result,'formsetFile': formsetFile,'formURL': formURL}
            response = render(request, 'pdfchecker/resultat.html', context)
            return  response
        if formsetFile.is_valid():
            formsetHandle.save()
            data = formsetFile.cleaned_data
            context = {'data':data}
            response = render(request, 'pdfchecker/resultat2.html', context)
            return  response
    else:
        formsetFile = UploadPdfFormSet()
        formURL = UploadPdfUrl

    response = render(request, 'pdfchecker/index.html', {
        'formsetFile': formsetFile, 'formURL': formURL,
        })

    page_visits = int(request.COOKIES.get('page_visits', '0'))

    if 'last_visit' in request.COOKIES:

        last_visit = request.COOKIES['last_visit']

        last_visit_time = datetime.strptime(last_visit[:-7], "%Y-%m-%d %H:%M:%S")

        if (datetime.now() - last_visit_time).days > 0:

            response.set_cookie('page_visits', page_visits+1)

            response.set_cookie('last_visit', datetime.now())

    if page_visits < 100:
        messages.add_message(request, messages.INFO, 'Donate information')
    else:

        response.set_cookie('last_visit', datetime.now())

    return response

我被困在哪里:

        if formsetFile.is_valid():
        formsetHandle.save()
        data = formsetFile.cleaned_data
        context = {'data':data}
        response = render(request, 'pdfchecker/resultat2.html', context)
        return  response

现在我知道这只会返回:

[{'docfile': <InMemoryUploadedFile: Getting Started.pdf (application/pdf)>}]

我需要的是返回上传文件的网址:

http://example.com/media/file.pdf

我认为它与MEDIA_URL有关。我如何'import' MEDIA_URL与视图相关?

0 个答案:

没有答案