我正在尝试将文件上传到我的django项目名称codewar中的/ media /文件夹中。我正在为每个用户创建一个独立的文件夹并将上传的文件放入其中。但是我的文件没有上传,我在提交查询后收到错误
TypeError at /index/
an integer is required
Request Method: POST
Request URL: http://127.0.0.1:8000/index/
Django Version: 1.7
Exception Type: TypeError
Exception Value:
an integer is required
Exception Location: C:\Python27\codewar\codewar\views.py in cust_proc, line 38
Python Executable: C:\Python27\python.exe
Python Version: 2.7.5
Python Path:
['C:\\Python27\\codewar',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time: Mon, 16 Feb 2015 17:29:28 +0530
我的urls.py是
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf.urls.static import static
from codewar.views import *
from django.conf import settings
urlpatterns = patterns('',(r'^index/$',mainsite),url(r'^admin/', include(admin.site.urls))) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += patterns('',) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我的views.py是
from django.shortcuts import *
from django.http import *
from django.template.loader import *
from django.template import *
from os import *
from os.path import *
from django.template import RequestContext
from django.conf import settings
def mainsite(request):
if request.method=='POST':
return render_to_response('index.html',context_instance=RequestContext(request,processors=[cust_proc]))
else:
return render_to_response('index.html',context_instance=RequestContext(request))
def cust_proc(request):
data={}
if request.method == 'POST':
newpath="C:\\Python27\\codewar\\media\\"+request.POST.get('name')
if not exists(newpath):
mkdir(newpath)
#t=save_file(request.POST.get('name'),request.FILES.get('browse'))
if 'file' in request.FILES:
file = request.FILES['file']
print file
# Other data on the request.FILES dictionary:
# filesize = len(file['content'])
# filetype = file['content-type']
dirname=request.POST.get('name')
filename = request.POST.get('filename')#file['filename']
s= settings.MEDIA_ROOT+"\\"+dirname+"\\"+filename
print s
#fd = open('%s' % (s), 'wb')
fd=request.FILES['file'].read()
fdd=open(s,"w")
ffd.write(fd)
fdd.close()
#fd.write(file['content'])
data={
'name':request.POST.get('name'),
'filename':request.POST.get('filename'),
'code':request.FILES['file'].read()
}
return {'data':data}
我的settings.py是:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'codewar.urls'
WSGI_APPLICATION = 'codewar.wsgi.application'
STATIC_URL = '/static/'
STATIC_ROOT = "C:\Python27\codewar\static"
TEMPLATE_DIRS=(
'C:\Python27\codewar',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
STATICFILES_DIRS = (
'C:\Python27\codewar\staticfiles',
)
MEDIA_ROOT = 'C:\Python27\codewar\media'
MEDIA_URL = '/media/'