VITALYS WEB
VITALYS WEB

Reputation: 839

NameError: name 'urlpatterns' is not defined

I'm trying to show an image using "ImageField"

from django.conf.urls.static import static
from django.conf import settings
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    )
MEDIA_ROOT = ''
MEDIA_URL = "/media/"

Upvotes: 2

Views: 8736

Answers (3)

zaidfazil
zaidfazil

Reputation: 9235

You need initialise urlpatterns first. Like,

urlpatterns = []

Then, you can do this,

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

set your settings.py like this,

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    )
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "/media/"

Upvotes: 1

amit_183
amit_183

Reputation: 981

try this:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

Upvotes: 0

Exprator
Exprator

Reputation: 27523

from django.contrib import admin
from django.conf.urls import include, url


urlpatterns=[
url(r'^admin/', include(admin.site.urls))
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

write this total code in your urls.py

Upvotes: 0

Related Questions