I have been following this post
However I do not understand this part in which the answer states
In url.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [....
]
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now
In my project if I add
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
after my
urlpatterns = [....
]
I get the error
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
TypeError: bad operand type for unary +: 'list'
any suggestions on how i can fix this ?
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Related
This is the error i am getting
template does not exist
This is my settings file I have added media root and media directory in settings.py
These are my urls urls
add this to your urls.py file
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
My uploaded images are not loading when uploaded to heroku and I think its not related to static file issue. When i set debug = False they come fine and i know that when it is false django uses itself to host the static assests. So setting that and my images loads fine but that's not the case if i set Debug = True on heroku or on my local env. Can somebody help me solve this issue.
template
<img class="img-fluid" style="height:auto;" src="{{post.work_img.url}}" alt="Generic placeholder image">
model
class WorkExp(models.Model):
work_img = models.ImageField(upload_to="work_images/")
app/urls.py
urlpatterns = [
path("carrer/", WorkExpView, name="carrer_page"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
project/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("blog.urls", namespace="blog")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In project/urls.py try this
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
instead of
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and set the debug = True
Check if you have static_root and are using python manage.py collectstatic.
I have a django rest project which is built on Django1.7. I need to run it on Django 1.11. When i run
python manage.py migrate
The error is:
ImportError: No module named urls
on url.py line
url(r'^docs/', include('rest_framework_swagger.urls')),
I have already made modifications in url.py file to avoid patterns. The url.py file look like
from django.conf.urls import include,url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic.base import TemplateView
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^grappelli/', include('grappelli.urls')),
url(r'^docs/', include('rest_framework_swagger.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'', include('gcm.urls')),
url(r'^', include('apps.account.urls')),
url(r'^', include('apps.vegetables.urls')),
url(r'^', include('apps.orders.urls')),
url(r'^', include('apps.listings.urls')),
url(r'^', include('apps.rating.urls')),
url(r'^', include('apps.faq.urls')),
url(r'^thank-you/$', TemplateView.as_view(template_name="thankyou.html"), name="thankyou"),
url(r'^/error/$', TemplateView.as_view(template_name="error.html"), name="error"),
url(r'^$', TemplateView.as_view(template_name="index.html"), name="home"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
'',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT})]
How could i run it?
In urls.py add this at the bottom,
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT
From Django 1.9 it does not support to add the URLs as a string, but it need to be imported as callable. So remove this from your urls.py.
urlpatterns += [ '', (r'^static/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT})]
I'm trying to serve user-uploaded images locally on Django 1.10. I'm following the documentation here and getting this error:
SystemCheckError: System check identified some issues:
Your URL pattern [<RegexURLPattern None ^media\/(?P<path>.*)$>] is invalid.
Ensure that urlpatterns is a list of url() instances.
The issue is from adding the static portion of my urls:
urlpatterns = [
...my urls...
]
if settings.DEBUG:
# This is causing the error.
urlpatterns += [
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
]
When I remove the static addition to my urls, the error goes away. What am I doing wrong here?
My applicable settings are as follows:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = BASE_DIR + "/static/"
STATIC_URL = "/static/"
MEDIA_ROOT = BASE_DIR + "/media/"
MEDIA_URL = "/media/"
The answer is that static should not be inside a list. This line:
urlpatterns += [
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
]
should be:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This serves the media files correctly:
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This doesn't serve the media files:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ ... ]
if settings.DEBUG:
urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The documentation writes "This is not suitable for production use!", so that is why I need a way to check for DEBUG before serving media files. How can I do that. Why does this approach doesn't work?
Use
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
(notice '=' after '+' - in your version you are adding static() patterns but not assigning the result to the urlpatterns variable)