jeudi 26 novembre 2015

Django HttpResponseRedirect(reverse()) with not url variable

How can I send the "not url related" variable when I use the HttpResponseRedirect(reverse())?

Below code is sample verifying module, and the syntax must be sent self

at the same time when user upload some file.

views.py :

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from myapp.models import Document
from myapp.models import upload_to_unqiue_folder
from myapp.forms import DocumentForm

def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()
 #############is vaild?#####################

      #      exVar = newdoc.docfile.name
        #    arg = /root/cSite/xxxxx
      #      exx = subprocess.Popen(arg, shell=True)
            filename = 'media/' + newdoc.docfile.name

            if(filename.split('.')[1]) != "png":
                 syntax = 'this is not PNG file'

            infile = open(filename, "rb")
            header = infile.read(8)

            if header != '\211PNG\r\n\032\n':
                syntax = 'not a valid PNG file'

            else:
                syntax = 'valid';


        # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('cSite.views.list'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(
        'list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )




Aucun commentaire:

Enregistrer un commentaire