Django-文件未上载到/media/<;用户>;文件
我正试图在django项目名称codewar中的/media/folder中上载文件。在这里,我为每个用户创建一个独立的文件夹,并将上载的文件放在其中。但是,我的文件没有被上传,在将查询提交为Django-文件未上载到/media/<;用户>;文件,django,python-2.7,file-upload,Django,Python 2.7,File Upload,我正试图在django项目名称codewar中的/media/folder中上载文件。在这里,我为每个用户创建一个独立的文件夹,并将上载的文件放在其中。但是,我的文件没有被上传,在将查询提交为 TypeError at /index/ an integer is required Request Method: POST Request URL: http://127.0.0.1:8000/index/ Django Version: 1.7 Exception Typ
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
我的URL.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)
我的观点是
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}
my 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/'
我认为第38行就是这一行:
fdd=open(s,"w")
请注意,您正在导入os.open
,在视图.py的开头使用此语句:
from os import *
在第38行中,您实际上正在尝试调用os.open
。现在,os.open
()不同于内置的open
()。它需要一个整数模式参数
您应该修改导入行,以便仅从os
库导入必要的函数
作为补充说明,不建议使用通配符导入,因为名称空间污染会导致此类错误。视图的第38行有什么内容?这是cust_proc
函数中的某个东西…filename=request.POST.get('filename')#file['filename']
这是从文本区域检索文件名的简单方法。文件['filename']已注释,代码仅为filename=request.POST.get('filename')。非常感谢您的帮助。。我让它工作了。。从昨天起,我就一直被困在这里面……)