Static and media files are not loaded in production - python

You can see my urls.py and my settings.py.
Server
NGINX, gunicorn
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('', include("appCms.urls"))
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
MEDIA_URL ='/mediafiles/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles_cdn/')

Related

Static files are not loading in Django

Why I am receiving files can't be found error while loading all the static files even after setting everything correctly.
ERROR
CODE:
Admin Section
- settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '')
- 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('', include('Welcome.urls')),
path('auth/', include('Authentication.urls')),
path('ad/', include('Ads.urls')),
path('user/', include('UserDashboard.urls')),
path('admin/', include('AdminDashboard.urls')),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Apps Section
- template
<link href="{% static 'css/user/style.css' %}" rel="stylesheet">
- Folder Structure
You get this error because the Django project searches your static folders in the wrong place.
This will work:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Static files couldnt applied in Django

I'm new in Django and i see course in youtube
I try to include bootstrap\css files into HTML template and i created a static file into the app like the photo
Also i checked the file(second\ urls.py) and in it the code
'''
STATIC_URL = '/static/'
INSTALLED_APPS = [
'accounts',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
'''
And in login.html there is this link
'''
{% load static%}
<link rel="=stylesheet" type="text/css" href="{% static 'accounts/style.css' %}">
'''
I applied other way like adding this second\setting.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR,"accounts/static"),
'accounts/static/'
]
And this is my urls.py file
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),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
In the terminal there is warning but i think it isnt affect
WARNINGS:
?: (2_0.W001) Your URL pattern '^account/' has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an
oversight when migrating to django.urls.path().
UPDATE: After looking in your application code, I've found the problem.
You have to put this code
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
in the main urls.py file , in your case is the second/urls.py .
And make sure you have this code
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'name_of_the_root_directory/static'),
]
in your settings.py file
Have you run the collectstatic command ? python manage.py collectstatic.
Maybe try changing this in your urls.py file
urlpatterns = [
url(r'^$', views.home),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Into this
urlpatterns = [
url(r'^$', views.home),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
This is my configuration in settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'name_of_the_root_directory/static'),
]
And this is my urls.py file
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),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
If you follow my example, you have to run the collectstatic command from the terminal window

Pycharm not making MEDIA folder

Settings and urls.py file ..... Pycharm not able to create MEDIA folder :/ what is happening .. please guide ?
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/media')
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('shop/', include('shop.urls')),
path('blog/', include('blog.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

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/"
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/"
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
try this:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

CKedit image upload not working

This is my urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views
from django.conf import settings
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/login/$', views.login, name='login'),
url(r'^accounts/logout/$', views.logout, name='logout', kwargs={'next_page': '/'}),
url(r'^ckeditor/', include('ckeditor_uploader.urls')),
url(r'', include('blog.urls')),
]
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
LOGIN_REDIRECT_URL = '/'
MEDIA_URL = '/media/'
CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_JQUERY_URL = 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'
Error code
I will upload image using CKeditor.
However, there is a 404 error.
What should I do?
To use the development server to deliver media files you need something like this in your urls.py
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
As far as I am aware Ckeditor does not add that automatically so you will need to ad this code to your urls.py

Categories

Resources