python - Displaying Images in Django - Setting up urls.py -
i apologize if duplicate. have been scouring web , tried multiple solutions. have built image uploader uploading images correct location but, think have in urls.py keeping me being able display images.
the commented lines attempts have made having no luck. urls.py:
from django.conf.urls import url . import views django.conf import settings django.conf.urls.static import static app_name = 'orders' urlpatterns = [ url(r'^$',views.index,name='index'), url(r'^invoice/$', views.invoice, name='invoice'), url(r'^photo/$', views.uploadview.as_view()), #static(settings.media_url, document_root=settings.media_root), #static(r'^media/', document_root=settings.media_root), #url("^media/(?p<path>.*)$","django.views.static.serve",{"document_root": settings.media_root}), url(r'^catalog/$', views.catalog, name='catalog'), url(r'^poststampsshipments/$', views.poststampsshipments, name='poststampsshipments'), url(r'^catalog/(?p<sku>[^/]+)/$', views.catalogdetail, name='catalogdetail'), url(r'^catalogchange/(?p<sku>[^/]+)/$', views.catalogchange, name='catalogchange'), url(r'^updateostkcat/$', views.updatecatalogostk, name='ostkcat'), url(r'^items/$', views.item, name='items'), url(r'^items/(?p<sku>[^/]+)/$', views.itemdetail, name='itemdetail'), url(r'^inventory/$', views.inventory, name='inventory'), url(r'^inventorychange/$', views.inventorychange, name = 'inventorychange'), url(r'^test/$', views.test, name='test'), url(r'^genbarcode/$', views.genbarcode, name='genbarcode'), url(r'^barcode/$', views.barcode, name='barcode'), url(r'^(?p<retailordernumber>[^/]+)/', views.orderdetail,name = 'detail'), url(r'^(?p<retailordernumber>[^/]+)/shipments/', views.shipments, name='shipments'),]
view:
def itemdetail(request,sku): edit = request.get.get('edit','') itm = item.objects.filter(sku=sku)[0] vendors = vendor.objects.all() cat = category.objects.all() template = loader.get_template('orders/itemdetail.html') context = { 'itemdetail':itm,'sku':sku,'edit':edit,'vendors':vendors,'cat':cat } return httpresponse(template.render(context, request))
settings.py:
media_url = 'media/' media_root = os.path.join(base_dir, 'media')
code in template:
<td>{% load static %} {{ img.image.url }} <img src="{{ img.image.url }}"> </td>
usual error seeing:
unhandled exception in thread started <function wrapper @ 0x0366aab0> traceback (most recent call last): file "c:\program files (x86)\anaconda2\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper fn(*args, **kwargs) file "c:\program files (x86)\anaconda2\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run self.check(display_num_errors=true) file "c:\program files (x86)\anaconda2\lib\site-packages\django\core\management\base.py", line 374, in check include_deployment_checks=include_deployment_checks, file "c:\program files (x86)\anaconda2\lib\site-packages\django\core\management\base.py", line 361, in _run_checks return checks.run_checks(**kwargs) file "c:\program files (x86)\anaconda2\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks new_errors = check(app_configs=app_configs) file "c:\program files (x86)\anaconda2\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config return check_resolver(resolver) file "c:\program files (x86)\anaconda2\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver pattern in resolver.url_patterns: file "c:\program files (x86)\anaconda2\lib\site-packages\django\utils\functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) file "c:\program files (x86)\anaconda2\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) file "c:\program files (x86)\anaconda2\lib\site-packages\django\utils\functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) file "c:\program files (x86)\anaconda2\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module return import_module(self.urlconf_name) file "c:\program files (x86)\anaconda2\lib\importlib\__init__.py", line 37, in import_module __import__(name) file "c:\mintjules\webapp\mintjules\mintjules\urls.py", line 25, in <module> url(r'^orders/',include('orders.urls')), file "c:\program files (x86)\anaconda2\lib\site-packages\django\conf\urls\__init__.py", line 50, in include urlconf_module = import_module(urlconf_module) file "c:\program files (x86)\anaconda2\lib\importlib\__init__.py", line 37, in import_module __import__(name) file "c:\mintjules\webapp\mintjules\orders\urls.py", line 25, in <module> url(r'^media/(.*)$', 'django.views.static.serve', {'document_root':os.path.join(os.path.dirname(__file__), 'static')}), file "c:\program files (x86)\anaconda2\lib\site-packages\django\conf\urls\__init__.py", line 85, in url raise typeerror('view must callable or list/tuple in case of include().') typeerror: view must callable or list/tuple in case of include().
look closely @ error stacktrace:
url(r'^media/(.*)$', 'django.views.static.serve',{'document_root':os.path.join(os.path.dirname(__file__), 'static')})
typeerror
: view must callable or list/tuple in case ofinclude()
.
django 1.10 no longer allows specify views string (e.g. 'django.views.static.serve'
) in url patterns.
the solution update urls.py
include view callable. means have import view in urls.py
:
from django.views.static import serve urlpatterns = [ url(r'^media/(.*)$', serve, {'document_root':os.path.join(os.path.dirname(__file__), 'static')}), # ... ]
this stated in the django docs.
Comments
Post a Comment