Project
myblog-
|
|-static
|
|-Css
|
|-bootstrap.css
** Setting.py**
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
**urls**
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from myblog.views import hello, current_datetime, hours_ahead
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/$',hello),
url(r'^time/$', current_datetime),
url(r'^time/plus/(\d{1,2})/$', hours_ahead),
url(r'^', include('blog.urls')),
url(r'^blog/',include('blog.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += staticfiles_urlpatterns()
Templates/base.html
{% load staticfiles %}
<link href="{% static 'base/css/bootstrap.css' %}" rel="stylesheet" type="text/css" >
After running manage.py runserver 192.168.8.213:8080. It shows Page not found (404) Request Method: GET Request
URL: http://192.168.8.213:8080/static/base/css/bootstrap.csenter code heres Raised by: blog.views.index
'base/css/bootstrap.css' could not be found
You're seeing this error because you have DEBUG = True in your Django
settings file. Change that to False, and Django will display a
standard 404 page.`enter code here`
We have to load static file into individual app, when we are on debug mode.
And In project name aPP folder, we will load the static folder
In setting.py
We have to define the path
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'myblog/static'),
)
In production
If we write "python manage.py colltecstatic" ,it will copy the static file in root static folder.
Related
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 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}),
I don't know why django can't load static files even though I have defined static_url, static_root, staticfiles_dirs and urlpatterns including static_root. I put static files in root_dir/static/ directory.
My templates also got right syntax with {% load static%} and {% static '...'%}
Please give me some advice on this.
Many thanks
This is my main urls file
from django.contrib import admin
from django.urls import path
from home import views as HomeViews
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',HomeViews.index,name='index')
]
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)
check if your program has the following :
settings.py :
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_DIR = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
dont forget to load this tag in your template file
{% load static %}
check for the call syntax
<link rel="stylesheet" href="{% static 'base.css' %}">
check if you have created the static folder in the base directory ( where you have your manage.py file)
Check if the contents in the static file and the call corresponds with each other (check spellings)
once try to make migrations and migrate
still if not working .... Try hard refresh (ctrl + F5)
In my application I was unable to serve static files like images and js.
Looks like everything is correct but still I am unable to figure out why it is not loading.
I have tried few options in stackoverflow none of them worked for me.
Settings.py
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static")
]
STATIC_URL = '/static/'
My project structure
mydjangonginx
|-mydjango_app
| -urls.py
| -views.py
| -...
|-mydjangonginx
| -urls.py
| -settings.py
| -...
|-static
| -images
| -login.jpg
| -js
| -my.js
mydjango_app/urls.py
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
path('login', views.login, name='login'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
mydjangonginx/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
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('mydjango_app.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
login.html
<html>
{% load static %}
login
<script src="{% static 'js/my.js' %}"></script>
<img src="{% static 'images/login.jpg' %}" />
</html>
both the js and images are not loading
can anyone help on this
The static folder should be next to manage.py in the same folder (this is BASE_DIR if you did not change it).
'django.contrib.staticfiles' is in your INSTALLED_APPS? And you are using the {% static 'folder/some.file' %} template tag to serve the static files?
Also note that adding staticfiles to the urlpatterns is not needed when running in DEBUG mode.
Edit: it has to be STATICFILES_DIRS not STATICFILES_DIR
I have troubles with upgrading my project from Django 1.8 to Django 1.10: static files are not loaded anymore.
My template looks like this:
{% load staticfiles %}
<!DOCTYPE html>
...
<link href="{%static 'file.css' %}" rel="stylesheet">
...
Then in my file settings.py, I have 'django.contrib.staticfiles' as an installed app. DEBUG is set to True, and I have:
STATIC_URL = os.path.join(BASE_DIR, 'static/')
STATIC_ROOT= os.path.join(BASE_DIR,'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), "./", ]
But when the html is produced, it is like the %static has no effect anymore. It is replaced by the empty string (the same works fine with Django 1.8, where the %static is replaced by the content of STATIC_URL). Does anyone know how to fix this ?
Can you added the Update the urls.py(mainproject/urls.py), once you made the chnages run the python manage.py collectstatic command.
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
===================
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
def root(folder):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',folder)
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
root('static'),
)