不支持Django按文件名导入

时间:2017-09-01 14:31:39

标签: python django windows powershell

我是django的新手,我正在尝试按照Need a minimal Django file upload example说明创建文件上传页面。但是,我一直得到"不支持导入文件"错误。

实际错误似乎在这里发生:

C:\Users\AppData\Local\Continuum\Anaconda\lib\importlib\__init__.py in import_module
            __import__(name) ...

完整追溯:

    Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.8
Python Version: 2.7.13
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'myapp')
Installed Middleware:
('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',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Users\..\AppData\Local\Continuum\Anaconda\lib\site-packages\django-1.8-py2.7.egg\django\core\handlers\base.py" in get_response
  119.                 resolver_match = resolver.resolve(request.path_info)
File "C:\Users\..\AppData\Local\Continuum\Anaconda\lib\site-packages\django-1.8-py2.7.egg\django\core\urlresolvers.py" in resolve
  368.                     sub_match = pattern.resolve(new_path)
File "C:\Users\..\AppData\Local\Continuum\Anaconda\lib\site-packages\django-1.8-py2.7.egg\django\core\urlresolvers.py" in resolve
  240.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "C:\Users\..\AppData\Local\Continuum\Anaconda\lib\site-packages\django-1.8-py2.7.egg\django\core\urlresolvers.py" in callback
  247.         self._callback = get_callable(self._callback_str)
File "C:\Users\..\AppData\Local\Continuum\Anaconda\lib\site-packages\django-1.8-py2.7.egg\django\utils\lru_cache.py" in wrapper
  101.                     result = user_function(*args, **kwds)
File "C:\Users\..\AppData\Local\Continuum\Anaconda\lib\site-packages\django-1.8-py2.7.egg\django\core\urlresolvers.py" in get_callable
  112.             if submod and not module_has_submodule(import_module(parentmod), submod):
File "C:\Users\..\AppData\Local\Continuum\Anaconda\lib\importlib\__init__.py" in import_module
  37.     __import__(name)

Exception Type: ImportError at /
Exception Value: Import by filename is not supported.

URLS.PY

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
import myapp

#from django_project import myapp
#from myapp import views as myapp_views

urlpatterns = [
    # Examples:
    # url(r'^$', 'django_project.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^$', 'http://127.0.0.1:8000/home/', name='home'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('myapp.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

MYAPP / URLS.PY

from django.conf.urls import patterns, include, url
#from django_project import myapp
import views as myapp_view


urlpatterns =  [
    #url(r'^list/$', include('myapp.views'))
    url(r'^list/$', myapp_view.list, name='list')
]

VIEWS.PY

from django.shortcuts import render

# Create your views here.
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.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()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('myapp.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(
        'myapp/list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )

1 个答案:

答案 0 :(得分:0)

这是您的错误发生的地方:

url(r'^$', 'http://127.0.0.1:8000/home/', name='home'),

作为评论者的建议,这应该是:

from .views import view_name
url(r'^$', view_name, name='home'),

详细了解其工作原理:https://docs.djangoproject.com/en/1.11/intro/tutorial03/#writing-more-views