从Django 1.7 - 1.8升级时,我得到ImportError:无法导入'serve'。路径必须完全合格

时间:2017-05-16 15:31:15

标签: django url upgrade importerror

我正在将我的项目从1.7升级到1.8,并且我想出了关于无法导入“服务”的问题。这发生在我的所有页面上,包括管理页面(减去显示的模板URL标签)。我在发行说明中找不到其他人有此错误或任何有关“发送”的说明。

有什么问题是什么?

Internal Server Error: /feed/
Traceback (most recent call last):
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\core\handlers\base.py", line 164, in get_response
response = response.render()
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\response.py", line 158, in render
self.content = self.rendered_content
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\response.py", line 135, in rendered_content
content = template.render(context, self._request)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\backends\django.py", line 74, in render
return self.template.render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\base.py", line 209, in render
return self._render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\base.py", line 201, in _render
return self.nodelist.render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\base.py", line 903, in render
bit = self.render_node(node, context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\debug.py", line 79, in render_node
return node.render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\loader_tags.py", line 135, in render
return compiled_parent._render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\base.py", line 201, in _render
return self.nodelist.render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\base.py", line 903, in render
bit = self.render_node(node, context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\debug.py", line 79, in render_node
return node.render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\loader_tags.py", line 135, in render
return compiled_parent._render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\base.py", line 201, in _render
return self.nodelist.render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\base.py", line 903, in render
bit = self.render_node(node, context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\debug.py", line 79, in render_node
return node.render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\defaulttags.py", line 329, in render
return nodelist.render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\base.py", line 903, in render
bit = self.render_node(node, context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\debug.py", line 79, in render_node
return node.render(context)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\template\defaulttags.py", line 493, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\core\urlresolvers.py", line 579, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\core\urlresolvers.py", line 433, in _reverse_with_prefix
self._populate()
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\core\urlresolvers.py", line 326, in _populate
lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\core\urlresolvers.py", line 247, in callback
self._callback = get_callable(self._callback_str)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\utils\lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "C:\Users\JBLT\Envs\django18\lib\site-packages\django\core\urlresolvers.py", line 103, in get_callable
lookup_view)
ImportError: Could not import 'serve'. The path must be fully qualified.

进料/ urls.py

from django.conf.urls import url, include
from django.conf import settings
from django.contrib import admin
from django.contrib.auth.decorators import login_required
from feed.views import *

urlpatterns = [
    url(r'^(?P<pk>[0-9]+)/hide/$', login_required(FeedPostHideView.as_view()), name='feed_post_hide'),
    #url(r'^$', login_required(PostHideView.as_view()), name='feed_post_hide'),
    url(r'^$', login_required(FeedPostListView.as_view(template_name='feed/feedpost_list.html')), name='feed_post_list'),
]

的src / urls.py

from django.contrib import admin
from django.contrib.auth.decorators import login_required
from django.views.generic import *
from django.conf.urls.static import static
from django.core.urlresolvers import reverse
from profile.models import Profile
from django.views.generic.base import TemplateView, RedirectView
from datetime import datetime
from django.conf.urls import url, include

urlpatterns = [
               url(r'^images/defaultavatar/$', RedirectView.as_view(url='/images/profiles/default_avatar.png'), name='default_avatar'),
                        url(r'^static/images/(?P<path>.*)$', 'serve',
                            {'document_root': settings.STATIC_ROOT + '/images/'}, name='images'),
                        url(r'^static/stylesheets/(?P<path>.*)$', 'serve',
                            {'document_root': settings.STATIC_ROOT + '/stylesheets/'}, name='stylesheets'),
                        url(r'^static/javascript/(?P<path>.*)$', 'serve',
                            {'document_root': settings.STATIC_ROOT + '/javascript/'}, name='javascript'),
                        url(r'^static/files/(?P<path>.*)$', 'serve',
                            {'document_root': settings.STATIC_ROOT + '/files/'}, name='files'),
                        url(r'^static/ads/(?P<path>.*)$', 'serve', {'document_root': settings.STATIC_ROOT + '/ads/'},
                            name='ads'),
                        url(r'^static/photos/(?P<path>.*)$', 'serve',
                            {'document_root': settings.STATIC_ROOT + '/photos/'}, name='photos'),
                        url(r'^static/icons/(?P<path>.*)$', 'serve',
                            {'document_root': settings.STATIC_ROOT + '/icons/'}, name='icons'),

                        # media for srd documents
                        url(r'^media/documents/(?P<path>.*)$', 'serve',
                            {'document_root': settings.MEDIA_ROOT + '/documents/'}, name='documents'),
    url(r'^admin/', include(admin.site.urls)),          
    url(r'^feed/', include('feed.urls')),
]

行给出错误:

{% url 'feed_post_list' %}

目前,我的项目布局如下:

PROJECT NAME
 PROJECT NAME
  TEMPLATES
   APP
    feedpost_flist.html
  urls.py
 APP
  urls.py

feed.urls无法找到模板文件会出现问题吗?

1 个答案:

答案 0 :(得分:0)

解决了它。用户在网址中使用“服务”。

urlpatterns = [
  url(r'^static/images/(?P<path>.*)$', 'serve',
]

更改为

from django.views.static import serve

urlpatterns = [
  url(r'^static/images/(?P<path>.*)$', serve,
]