简体   繁体   中英

Displaying Images in Django - Setting up urls.py

I apologize if this is a duplicate. I have been scouring the web and tried multiple solutions. I have built an image uploader that is uploading the images to the correct location but, I think I have something in my urls.py that is keeping me from being able to display the images.

The commented lines are attempts i have made but i am having no luck. urls.py:

from django.conf.urls import url
from . import views
from django.conf import settings
from 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 i am seeing:

Unhandled exception in thread started by <function wrapper at 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
    for 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 be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().

Look closely at your error stacktrace:

url(r'^media/(.*)$', 'django.views.static.serve',{'document_root':os.path.join(os.path.dirname(__file__), 'static')})

TypeError : view must be a callable or a list/tuple in the case of include() .


Django 1.10 no longer allows you to specify views as a string (eg 'django.views.static.serve' ) in your URL patterns.

The solution is to update your urls.py to include the view callable. This means that you have to import the view in your urls.py :

from django.views.static import serve

urlpatterns = [
    url(r'^media/(.*)$', serve, {'document_root':os.path.join(os.path.dirname(__file__), 'static')}),
    # ...
]

This is also stated in the Django docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM