我是django框架中的新手 尝试为文件系统中的上传图像构建模板,并在页面上显示此图像。 首先我得到错误首先我在文件中得到错误
"/home/tac/Desktop/myproject/myapp/urls.py", line 5, in <module>
url(r'^$', 'list', name='list'),
文件“/usr/local/lib/python2.7/dist-packages/django/conf/urls/init.py”,第85行,在网址中 引发TypeError('视图必须是可调用的,或者在include()的情况下是list / tuple。') TypeError:在include()。
的情况下,view必须是可调用的或list / tuple不知何故,我解决了这个错误,但在那之后,我面临另一个错误 enter image description here
的myapp / urls.py
from django.conf.urls import include, url
urlpatterns = ('myapp.views',
url(r'^$', list, name='list'),
url(r'^list/$', list, name='list'),
)
的myproject / urls.py
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
from django.contrib import admin
# admin.autodiscover()
urlpatterns = [
(r'^myapp/', include('myapp.urls')),
(r'^$', 'myapp.views.index'),
(r'^admin/', include(admin.site.urls)),
]
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
错误 开始的线程中的nhandled异常 Traceback(最近一次调用最后一次): 文件“/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py”,第226行,在包装器中 fn(* args,** kwargs) 在inner_run中输入文件“/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py”,第121行 self.check(display_num_errors =真) 文件“/usr/local/lib/python2.7/dist-packages/django/core/management/base.py”,第420行,正在检查中 提升SystemCheckError(msg) django.core.management.base.SystemCheckError:SystemCheckError:系统检查发现了一些问题:
错误: ?:( urls.E004)您的网址格式('^ $','myapp.views.index')无效。确保urlpatterns是url()实例的列表。 提示:尝试使用url()而不是元组。 ?:( urls.E004)您的网址格式('^ admin /',([,,,,,, \ d +)/(?P。+)/ $&gt;,(无:无)^ auth / user / &gt;,(无:无)^ auth / group /&gt;,auth)/ $&gt;],'admin','admin'))无效。确保urlpatterns是url()实例的列表。 提示:尝试使用url()而不是元组。 ?:( urls.E004)您的网址格式('^ myapp /',(,无,无))无效。确保urlpatterns是url()实例的列表。 提示:尝试使用url()而不是元组。
系统检查发现3个问题(0个沉默)。
答案 0 :(得分:0)
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
from django.contrib import admin
# admin.autodiscover()
urlpatterns = [
url(r'^myapp/', include('myapp.urls')),
url(r'^$', 'myapp.views.index'),
url(r'^admin/', include(admin.site.urls)),
]
urlpatterns+= static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 1 :(得分:0)
myproject/urls.py
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
from django.contrib import admin
# admin.autodiscover()
urlpatterns = [
url(r'^myapp/', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
urlpatterns+= static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
myapp/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# url(r'^$', views.list, name='list'),
url(r'^$', views.index, name='index'),
url(r'^list/$', views.list, name='list'),
]