settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = '../media/images/'
urls.py
if settings.DEBUG:
# Use static() to add url mapping to serve static files during development (only)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py
profile_image = models.ImageField(upload_to="../media/images")
file path:
-main_project
/app #django app
/media/images #media folder inside image folder
/main #django main app
/static/css #static folder for js,css,images,fonts
/templates #html templates
I have also tried other way but still, my image is not uploaded in media folder
Change MEDIA_URL and MEDIA_ROOT to:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
Once you define above you don't need to explicitly put media folder inside ImageField:
profile_image = models.ImageField(upload_to="images")
See the official docs about managing files
firstly change to this
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
and in you model
profile_image = models.ImageField(upload_to="images")
and i guess you are using form to upload the image so in that
<form method="POST" enctype="multipart/form-data">
use enctype and if still doesn't work for you try to update it from admin panel than tell me it update or not
Related
My site can't locating where it's static and media file after upload on shared hosting. Here is my
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/project32/rootfolder/media/' #this is the path of my hosing media root file
STATIC_ROOT = '/home/project32/rootfolder/static/' #this is the path of my hosing static root file
root urls.py
urlpatterns = [
path('admin/', admin.site.urls),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
where I am doing mistake? why my website can't locating the static folder on my shared hosing ?
I also run python manage.py collectstatic
my problem solved after adding adding STATIC_URL path in my root urls.py
urlpatterns = [
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)
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
You should delete the 'STATIC_ROOT' line. You should use it this way.
It is a simple static html file with only one image as a static file in the html. I have followed all the steps provided in the Django documentation, and the collectstatic command runs without any error.
The project folder structure is as follows;
settings.py file;
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
DEBUG = True
home.html - File where the image is not loading;
{%load static%}
<!DOCTYPE html>
<html >
<head>
<title>home.html</title>
<h1>This is a test file</h1>
</head>
<body>
<img src="{% static 'images/test.png' %}">
</body>
</html>
Output:
I have tried the following;
Restart server,
rerun the collectstatic command
Nothing worked out, What could be the issue? Please help me to resolve this issue.
replace STATICFILES_DIR with STATICFILES_DIRS in settings.py
You have STATIC_ROOT = os.path.join(BASE_DIR, 'assets') whereas it looks like you've already collected your files to os.path.join(BASE_DIR, 'static'), which is a more typical location. You should change STATIC_ROOT to point back to the static folder so that Django knows where to serve those files from:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
You can see this in the static files deployment guide
try this in your settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
def get_success_url(self, request, user):
return (get_success_url)
and add this from your urls.py
urlpatterns = [
......
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns +=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
STATICFILES_DIR is the main error here.
Replace it with STATICFILES_DIRS
This should solve it.
I have a django project based on django 1.11.
I uploaded media files and try to get them but instead I get nothong.
I read documentation and did exactly as it said.
Also I checked 'media' foleder permissions.
settings.py:
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('myapp.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Change your MEDIA_ROOT to MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') in settings.py and make sure your media file in project root directory.
Hi I am trying to get user profile images to display from a Django MySQL database corresponding to the user that is logged in..
Here is my settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
MEDIA_DIR = os.path.join(BASE_DIR,'media')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]
# Media files
MEDIA_ROOT = MEDIA_DIR
#MEDIA_DIRS = [MEDIA_DIR, ]
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/admin/media/'
Here is my urls.py:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^smartcity/',include('smartcity.urls')),
#url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
#url(r'^accounts/register/$', views.register, name='registration'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^admin/', admin.site.urls),
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]
And this is what I have in the html file:
src="{% static 'user.userprofile.picture.url' %}"
This is my table i am trying to retrieve the picture from:
Database screenshot
I'm not sure how to get it to display, I thought my URL mapping was correct as i can view the image if i go to http://127.0.0.1:8000/media/profile_images/trump.jpg
Any ideas? Sorry I am a bit of noobie.
FileField uploads to the media side of things, not static, so you just have to do;
<img src="{{ user.userprofile.picture.url }}" alt="" />
Here is an example of this scenario; https://www.simplifiedpython.net/django-file-upload-tutorial/
I tried to handle this but I gave up. I have folder with images and I want to display my some image in html view but It wont work.
I followed this tutorial enter link description here
this my project tree:
As you can see I tried to create plenty of directories to make it work.
This is my settins:
STATIC_URL = '/static/'
# STATICFILES_DIRS = [
# os.path.join(BASE_DIR, "media"),
# '/webstore/',
# ]
MEDIA_ROOT = '/webstore/media/'
MEDIA_URL = '/media/'
this is my html view where I try to display my image
<img src="/media/example.jpg" />
this is my urls.py file
from django.conf.urls import url, include
from django.contrib import admin
from djangoproject import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^', include('webstore.urls')),
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
MEDIA_URL is the base URL to serve the media files uploaded by users, and MEDIA_ROOT is the local path where they reside.
so try to use it on your setting.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
and on your main urls.py
urlpatterns = [
....
]
if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and should the entire image will be saved in your djangoproject/media/
one more thing don't forget to add {% load staticfiles %} on top your html file. {% load staticfiles %} tells Django to load the staticfiles template tags that are provided by the django.contrib.staticfiles application.
MEDIA_ROOT is the absolute filesystem path to the directory that will hold user-uploaded files.
STATIC_ROOT is the absolute filesystem path to the directory from which you’d like to serve these files.
Since, you would like to serve images, give the absolute path of your static directory to STATIC_ROOT.
Give relative path with respect to your STATIC_ROOT in STATIC_URL.
Also, change your urls.py with the static_url and static_root.
Another Suggestion: Easier way to display an image is to upload it to image servers like imgur and give the url of the image in the html.
For e.g. :
<img src="http://i.stack.imgur.com/nbegK.png" />
The only thing you have to do is
MEDIA_ROOT = '/absolutepath/to/djangoproject/webstore/media/'
Then you have already
MEDIA_URL = '/media/'
and try
<img src="/media/example.jpg" />