python3+django3 TypeError: 'URLPattern' object is not iterable

解决Django项目中图片展示问题,正确配置URLPattern,避免TypeError错误。对比append()与'+='操作符在列表操作上的差异。

最近在使用django3+开发应用时,需要在页面中展示图片,这时需要根据不同的条件在项目根目录urlpatterns列表里面添加url。 话不多说直接上代码:

if DEBUG:
    	urlpatterns += url(r'^media/(?P<path>.*)/$', serve, {'document_root': MEDIA_ROOT})

但是在启动项目的时候一直提示:

TypeError: 'URLPattern' object is not iterable

搞了好久终于想到在python3中有直接对列表添加元素的方法 - append()方法
将上面的代码更改为:

if DEBUG:
    urlpatterns.append(url(r'^media/(?P<path>.*)/$', serve, {'document_root': MEDIA_ROOT}))

项目成功启动!

扩展:
python列表中append方法是给列表增加一个元素,而‘+=’是增加上该符号后边的元素,可以是给字符串拆开来拆成单个字符来插入。

example:
testlist = ['zhangsan', 'list']
for i in testlist:
    print(i)
print('++++++++++')
testlist += 'wangwu'
for i in testlist:
    print(i)

输出结果:
zhangsan
list
++++++++++
zhangsan
list
w
a
n
g
w
u
看到这个有点懵b
而使用append()方法是时:

testlist = ['zhangsan', 'list']
for i in testlist:
  		print(i)
print('+++++++++++')
testlist.append('wangwu')
for i in testlist:
   	print(i)

输出结果:
zhangsan
list
+++++++++++
zhangsan
list
wangwu
完全符合预期!

(myenv1) mo@mo-virtual-machine:~/myproject$ python manage.py runserver Watching for file changes with StatReloader Performing system checks... Exception in thread django-main-thread: Traceback (most recent call last): File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner self.run() File "/usr/lib/python3.6/threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "/home/mo/myenv1/lib/python3.6/site-packages/django/utils/autoreload.py", line 64, in wrapper fn(*args, **kwargs) File "/home/mo/myenv1/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run self.check(display_num_errors=True) File "/home/mo/myenv1/lib/python3.6/site-packages/django/core/management/base.py", line 423, in check databases=databases, File "/home/mo/myenv1/lib/python3.6/site-packages/django/core/checks/registry.py", line 76, in run_checks new_errors = check(app_configs=app_configs, databases=databases) File "/home/mo/myenv1/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config return check_resolver(resolver) File "/home/mo/myenv1/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver return check_method() File "/home/mo/myenv1/lib/python3.6/site-packages/django/urls/resolvers.py", line 412, in check for pattern in self.url_patterns: File "/home/mo/myenv1/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/home/mo/myenv1/lib/python3.6/site-packages/django/urls/resolvers.py", line 598, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "/home/mo/myenv1/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/home/mo/myenv1/lib/python3.6/site-packages/django/urls/resolv
最新发布
03-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值