Can not create media folder in Django Project - python

Media folder is not getting created when I run the server. I am working in localhost.
You can see the urls.py and settings.py code below.
I added 'django.template.context_processors.media' to the templates. My Django Version is 2.0.3 and I am using Python 3.6.8.
When I run the code, media folder should be created automatically. How can I fix that issue?
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') #settings.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name = "index"),
path('about/', views.about, name = "about"),
path('articles/', include("article.urls")),
path('user/', include("user.urls")),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #urls.py

For a media file to be created,you need to add a slash after 'media' in the MEDIA_ROOT
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
instead of:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Related

Why 404 error occurs even though I have set STATIC_ROOT and urls.py

I don't know why, but I get 404 errors when trying to use static file like this: {% static 'js/some.js' %}.
Here's my urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path("account/", include("account.urls")),
path("", include("post.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
And this is my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static/'
# media config
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / 'media/'
Thanks!!
EDIT
Here's the error I get
GET http://127.0.0.1:8000/static/js/some.js net::ERR_ABORTED 404 (Not Found)
And the url you can see here is right.
Please try this:
# settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]

Django Unable to load static files - internal-nginx-static comes in URL

Hope you are all having a very good Friday!
I am running a django project on nginx, and my project is each time going into this url - https://sitename.com/internal-nginx-static-location_uat/project_name/static/images/success.svg
I didn't configure this internal-nginx-static, and If I load the same project on another website, it works fine, is it something I can handle in code, or I have make changes in server conf.
Here is my URL.py Files
from django.conf import settings
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path, include
urlpatterns = [
path('', include('portal.urls')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and settings file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(DATA_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Media directory in production not working

The media directory is working as expected in my local machine but when I push the code to Heruko or Cloud Foundry the media is not working.
How to rectify this issue?
urls.py
urlpatterns = [
url(r'^profile/$', view_profile, name='view_profile'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
MEDIA_DIR = os.path.join(BASE_DIR, "media")
# Medta files location
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'

can't get media files in heroku

I deployed mu project in heroku successfully.
The only problem is that I can't find media files on heroku.
When I type .../media/pic1.png locally , I get the picture in the browser.
But, in heroku,, that gives
Page not found (404)
Request Method: GET
Request URL:
...../media/pic1.png
Raised by: django.views.static.serve
Path ...../media/pic1.png doesn't exist
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
project/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^admin_platform/', include('admin_platform.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Your setting.py should look something like this:-
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')#by doing this there will be media folder in your main directory.
And in your url.py your code should be like this:-
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^admin_platform/', include('admin_platform.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)#this will help to access your media folder.
Hope this help in your project. :-)

Static Files With Django Development Server 1.7 Slightly Different Directory Settings

This is my website directory:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\js
\css
\media
\templates
base.html
What I have added to my settings.py is:
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "templates"),
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static"),
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media"),
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static"),
),
and to my 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 hey, it does'nt work. The way I refer to them in the base.html is:
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
Any ideas?
Thank you.
Hasan
Running the settings that you've pasted gave me an error ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting. Commenting-out os.path.join(os.path.dirname(BASE_DIR), "static"), in the settings removed the error and the template correctly found the CSS file without the URLs.py modifications.
First of all as #r---------k mentioned, I should not have had trailing commas except for the tuples. So the settings.py should look like:
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
And the second thing is that, as you may notice, STATIC_ROOT and STATICFILES_DIRS cannot possibly have the same value. I added another static folder inside the static folder and put my js and css folders inside of it:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
The directory looks like:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\static
\js
\css
\media
\templates
base.html
Finally, you should have the urlpatterns in your 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)
This seems to work in current django (2.2) in your urls.py:
from django.conf import settings
from django.conf.urls.static import serve
from django.urls import include, path
urlpatterns += [
path(settings.STATIC_URL[1:], serve, {'document_root': settings.STATIC_ROOT })
]
Remember to run ./manage.py collectstatic to collect the static files to settings.STATIC_ROOT

Categories