def uploadHandler(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) return HttpResponseRedirect('/success/url/') else: form = UploadFileForm() return render(request, 'upload.html', {'form': form})
def upload_file(request): if request.method == "POST": form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES["file"]) return HttpResponseRedirect("/success/url/") else: form = UploadFileForm() return render_to_response("upload.html", {"form": form})
def upload_file(request): if request.method != 'POST': return HttpResponseNotAllowed('Only POST here') form = UploadFileForm(request.POST, request.FILES) if not form.is_valid(): return HttpResponseServerError("Invalid call") handle_uploaded_file(request.FILES['file']) return HttpResponse('OK')
def upload_file(request): if request.method != 'POST': return HttpResponseNotAllowed('Only POST here') form = UploadImageForm(request.POST, request.FILES) if not form.is_valid(): return HttpResponseServerError("Invalid call") handle_uploaded_file(request.FILES['file']) return HttpResponse('OK')
def inventory(request): if not request.user.is_authenticated(): return render(request, 'foodpantry/login.html') else: if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) else: form = UploadFileForm() return render(request, 'foodpantry/test.html', {'rows':reverseTable()})
# A view handling this form will receive the file data in request.FILES, which is a dictionary containing a key for each FileField (or ImageField, or other FileField subclass) in the form. So the data from the above form would be accessible as request.FILES['file']. from django.http import HttpResponseRedirect from django.shortcuts import render from .forms import UploadFileForm # Imaginary function to handle an uploaded file. from somewhere import handle_uploaded_file def upload_file(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) return HttpResponseRedirect('/success/url/') else: form = UploadFileForm() return render(request, 'upload.html', {'form': form}) 五.Django shortcut functions from django.http import HttpResponse from django.template import loader def my_view(request): # View code here... t = loader.get_template('myapp/index.html') c = {'foo': 'bar'} return HttpResponse(t.render(c, request), content_type='application/xhtml+xml')