Django-文件未上传到/media/<user> 文件中

Django- Files are not being uploaded in /media/<user> file

我正在尝试将文件上传到我的 django 项目名称中的 /media/ 文件夹中 codewar.Here 我正在为每个用户创建一个独立的文件夹并将上传的文件放入其中。但是我的文件没有被上传,并且在提交查询后出现错误

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/'

我认为第38行是这一行:

fdd=open(s,"w")

请注意,您在 views.py:

的开头使用此语句导入 os.open
from os import *

在第 38 行中,您实际上是在尝试调用 os.open。现在,os.open (docs) is different than built-in open (docs)。它需要一个整数模式参数。

您应该修改导入行以仅从 os 库中导入必要的函数。

附带说明一下,不建议使用通配符导入,因为命名空间污染会导致此类错误。