I want to generate thumbnails using solr_thumbnail but i am getting error .
I have followed the tutorial and generated the thumbnail and saved under cache folder. But now it is looking under /home/vaibhav/TRAC/flipdiscounts/flipdiscounts/flipdiscountApp/images/cache/b2/f7/b2f7287e13292d73659a0fde830ef197.jpg/
urls.py :
url(r'^images/(?P<path>.*)/$', 'django.views.static.serve', # For store image
{'document_root': MEDIA_ROOT, 'show_indexes': False}),
can someone help me ...
try setting your MEDIA_ROOT root to /home/vaibhav/TRAC/flipdiscounts/flipdiscounts/flipdiscountApp/images/
and set your MEDIA_URL to /images/
and use this in your urls.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Related
I am developing a website with Django, I had a lot of pictures in my project, uploaded from the admin panel and saved in the Media folder which I created for these uploads separately, It was working fine and exact way I wanted in months, Suddenly they are just not loading, getting 404 for all of them, without any change in project, they are just not loading.
My media path in Settings.py :
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
I have added this to the end of my urls.py of the app:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and as i said, it was working fine for a long, suddenly this happened
edit: I just figured it out that this is happening when I am using redirect function in one of my views
Another way to do it is:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# other URLs ...
]
# If DEBUG=True in the settings file
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
So we're adding static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to the urlpatterns if DEBUG=True in the settings.py file. This gives a better management in some sense.
I had the same issue and this helps me :
Add this in your urls.py in the urlpatterns list :
from django.views.static import serve
from django.conf import settings
from django.conf.urls import url
urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
# YOUR URLS ARE HERE
]
Of course stay in Debug=True in settings.py.
I have some product pictures uploaded in media folder that displays fine in development server, but after deployment I receive the 404 ressource not found error.
In settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'node_modules')]
STATIC_ROOT = os.path.join(BASE_DIR, 'productionStatic')
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
In urls.py
from django.contrib import admin
from django.urls import path
from index import views
from index.views import indexPage, hdnytorv
from webshopRestaurant.views import hd2900_webshop_Main, AddressCheckForDeliverability, ChangeItemQuantity
from webshopCustomer.views import TakeawayCheckout, totalPriceDeliveryPossible, DeliveryForm, PickUpForm, localDeliveryCheckoutAddressCheck, Payment, PaymentComplete
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
path('', indexPage.as_view(), name = 'index'),
path('hdnytorv', hdnytorv.as_view(), name='hdnytorv'),
path('hd2900', views.hd2900.as_view(), name='hd2900'),
path('hd2900_takeaway_webshop', hd2900_webshop_Main.as_view(), name="hd2900_takeaway_webshop"),
path('check-address-for-deliverable', AddressCheckForDeliverability.as_view()),
path('changeItemQuantityInBasket', ChangeItemQuantity.as_view()),
path('isPriceAboveDeliveryLimit', totalPriceDeliveryPossible.as_view()),
path('hdbynight', views.hdbynight.as_view(), name='hdbynight'),
path('takeawayCheckout', TakeawayCheckout.as_view()),
path('deliveryFormCheckout', DeliveryForm.as_view()),
path('pickupFormCheckout', PickUpForm.as_view()),
path('local_delivery_checkout_is_address_deliverable', localDeliveryCheckoutAddressCheck.as_view()),
path('localDeliveryPayment', Payment.as_view()),
path('paymentComplete', PaymentComplete.as_view()),
]
#When in production medida url must always be added to urlpatterns
#if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
To display the product in view I have in my html template
<img class="card-img embed-responsive-item" src="{% get_media_prefix %}{{ item.product.image_path }}" alt="{{item.meta_description}}">
The pictures are uploaded to this location django_project_folder/media/productImages. I have assured that it is owned by www-data group and also media and all subfolders have 775 access rights. In development server the images shows up all right but in production I got 404 not found error.
This issue is a duplicate. See the answer here I fixed it by adding in urls.py
from django.urls.conf import re_path
from django.views.static import serve
re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
This question already has an answer here:
how to upload file in a specific address using django admin panel?
(1 answer)
Closed 4 years ago.
I want to show the uploaded image in the django templates,but it was not showing any image,i have successfully uploaded and saved it,how can i fix this problem,could anyone please explain or a sample demo on how we have to configure our settings.py to display the image in templates?
my settings.py:
LOGIN_URL='/login'
STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/home'
LOGOUT_REDIRECT_URL='/accounts'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
my models.py:
class Images(models.Model):
user_id=models.ForeignKey(User,on_delete=models.CASCADE,null=True)
image=models.FileField(upload_to='home')
And the Image tag i was using to display it is
<img src="{{ obj.image.url }}" class="img-circle" height="65" width="65" alt="Avatar">
my urls.py:
from django.urls import path
from . import views
from . models import FavBooks
from django.conf import settings
from django.conf.urls.static import static
app_name='home'
urlpatterns=[
path('',views.HomeView,name='home'),
path('addBooks/',views.addBooks,name='addBooks'),
path('uploadpic/',views.uploadPic,name='uploadPic'),
path('myBooks/',views.BooksView.as_view(),name='myBooks'),
path('<int:pk>/', views.BookDetailsView.as_view(), name='myBooks'),
path('search/', views.SearchedBooks, name='searchedBooks'),
path('favoriteAjax/', views.favorite_ajax, name='favoriteAjax'),
path('removefavoriteAjax/', views.removefavorite_ajax, name='removefavoriteAjax'),
path('wishList/',views.WishlistView.as_view(),name='wishList'),
path('deletebook/', views.deleteBook, name='deleteBook'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
add this in urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have correctly set the settings for media in settings.py as:-
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and also specified the correct url settings
if settings.DEBUG:
urlpatterns += patterns(
'django.views.static',
(r'^media/(?P<path>.*)',
'serve',
{'document_root': settings.MEDIA_ROOT}), )
Whenever i upload an image through admin..the image gets uploaded in my media folder but i can't see it when i run my development server...it shows a page not found (404) error...
I think the problem is due to some permissions of uploaded files which is why django shows a 404 page when trying to access them..if someone knows about this please help
Please suggest me a solution
Thank you!
Try this:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Relevant url:
https://docs.djangoproject.com/en/1.7/howto/static-files/#serving-files-uploaded-by-a-user-during-development
Do this
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = patterns('',
# your urls here.
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I know this question was asked many times, but none of the answers i found and tried helped me.
Those are my static files settings:
STATIC_ROOT = os.path.abspath(SETTINGS_PATH+'/staticfiles/')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/staticfiles/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(os.path.dirname(__file__), 'static'),
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
And in myapp/urls.py:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = patterns('',
# urls
)
urlpatterns += staticfiles_urlpatterns()
Collectstatic copies all files to staticfiles/ as it should and i get 404 on all static files.
I also tried this in urls.py:
if not settings.DEBUG:
urlpatterns += patterns('',
url(r'^staticfiles/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)
This gives me following kind of errors:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/css/bootstrap.css". localhost:11
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/css/style.css". localhost:12
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/js/bootstrap.js". localhost:79
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/js/login.js".
I can't see what's wrong with my settings. Any ideas are most welcome.
as you can see in the warning box in the docs, in production (i.e. with debug=False) you should be using your web server to serve static files, not django. For that reason, staticfiles will refuse to serve your assets if debug=False.
in settings.py::
if DEBUG:
STATIC_ROOT = os.path.join(BASE_DIR, '/static')
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
and in urls.py add
import django.views.static
urlpatterns += [
url(r'^static/(?P<path>.*)$', django.views.static.serve, {'document_root': settings.STATIC_ROOT, 'show_indexes': settings.DEBUG})
]
I'm using the code below in my root urls.py. You need to set FORCE_SERVE_STATIC to True in settings file if you want static and media to be served by django development server. And don't forget to run collectstatic command to update your static files.
from django.conf.urls.static import static
from django.conf import settings
<...>
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
elif getattr(settings, 'FORCE_SERVE_STATIC', False):
settings.DEBUG = True
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(
settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.DEBUG = False
This is a late answer but may be this will help someone. There is an extra option to be used with runserver command which force Django to serve static files even when DEBUG=False
python manage.py runserver --insecure
Django docs ref: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#cmdoption-runserver-insecure
In your urls.py file:
add this line
from django.views.static import serve
add those two urls in urlpatterns:
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
With debug turned off Django won't serve static files for you any more .
Your production web server (Apache or Heroku or Pythonanywhere or something like this) should take care of that.
from django.views.static import serve
from django.conf import settings
if not settings.DEBUG:
urlpatterns += re_path(
r'^static/(?P<path>.*)$', serve, dict(document_root=settings.STATIC_ROOT)),
Note:
django.conf.urls.url() is deprecated in favor of django.urls.re_path().
def url(regex, view, kwargs=None, name=None):
warnings.warn(
'django.conf.urls.url() is deprecated in favor of '
'django.urls.re_path().',
RemovedInDjango40Warning,
stacklevel=2,
)
return re_path(regex, view, kwargs, name)