I have been trying to test my Django project before deploying it on a cpanel
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
====== in project urls.py ========
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
urlpatterns = [
path("", include("myapp.urls")),
path('admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns modification to serve static files is recommended in development.
If you want to serve your static files from the same server that’s already serving your site, the process may look something like:
Push your code up to the deployment server.
On the server, run collectstatic to copy all the static files into
STATIC_ROOT.
Configure your web server to serve the files in STATIC_ROOT under
the URL STATIC_URL. For example, here’s how to do this with Apache
and mod_wsgi.
How to use Django with Apache and mod_wsgi
Related
when deployed server then static files not working and media files not diplaying. 404 error
here is the urls.py
from django.views.static import serve
import django
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path ('' , include('home.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and settings.py
STATIC_URL = 'static/'
STATIC_ROOT = '/usr/local/lsws/Example/html/demo/static'
"""STATICFILES_DIRS=(
BASE_DIR / "static",
)"""
MEDIA_URL = 'media/'
MEDIA_ROOT = '/usr/local/lsws/Example/html/demo/static/media'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CKEDITOR_UPLOAD_PATH = 'uploads'
Verify that on prod it's the same folder and the user running webserver has access to this folder. Consider using BASE_DIR instead of absolute paths. Also you might prefer to store projects files not inside user private home folder and store them somewhere /var/opt or whatever. If you had media files locally before deployment then they must be copied to prod manually. –
Thank you #Ivan-Starostin
I created static directory which included css and image files in my Django project. When I run my project the css do display("GET /static/website/barber.css HTTP/1.1" 200 69) but the images do not("GET /static/website/images/balloons.gif HTTP/1.1" 404 1840),
in your setting.py file define STATIC_URL
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static'),
os.path.join(BASE_DIR,'boot'),
]
STATTC_URL = '/static/'
and add this to your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#your url
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Add STATIC settings
You need to create these definitions in settings.py to tell Django where is static files:
STATIC_DIR = os.path.join (BASE_DIR, "static")
STATIC_ROOT = os.path.join (BASE_DIR,'static files')
STATIC_URL ='/static/'# path to read css with local (probably)
STATICFILES_DIRS = [
os.path.join (BASE_DIR, "static"),
]
Be sure to import settings and static in urls.py.
You also need to add them to your main urls.py to access the URLs of images:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#Your urls
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have excel files in my static folder that I read from to get some data but the files arent there when I deploy my project
these are my settings for configuring static files
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
this is how I call them in my views
from django.conf import settings
from django.templatetags.static import static
from django.contrib.staticfiles.storage import staticfiles_storage
from django.contrib.staticfiles.finders import find
def get_static(path):
if settings.DEBUG:
return find(path)
else:
return static(path)
def sport_news(request):
path = 'excel\sport_update.xlsx'
excel_data = excel(get_static(path))
return render(request, 'news/news.html', {"excel_data":excel_data, 'type':'Sport News'})
please check in you settings.py
but first
pip install whitenoise
in middleware
'whitenoise.middleware.WhiteNoiseMiddleware',
in bottom
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
then your main projects 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)
and please make sure you do have a static cdn folder in your settings.py
then run python manage.py collectstatic
if you done all the thing mentioned above and still file is not please make sure that file is in the static cdn folder and if not push the code again after completing all the step and you will be good to go
if things still did not work for you tell me
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}),
Ok, I have a Django 1.10 project. The relevant settings look like this:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = BASE_DIR + "/media/"
MEDIA_URL = '/media/'
I'm working locally, I can upload images correctly. But when I try to access the image on a template using {{ image.image.url }}, I get a 404. In the terminal I can see this:
[06/Sep/2016 18:13:43] "GET /media/folder/uploaded_image.jpg HTTP/1.1" 404 4900
But if I look into my folder, the file is there, correctly uploaded by django.
Try using os.path.join, like this:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
You probably also need to update your urls.py with this:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)