I am facing some very basic issue.
I saved some detail in my django model including a profile picture.
When i open django admin then i can saw i link for the image which i have saved.But when i click on that link it show me error.My image is not founding.
How can i resolve the issue.
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "staticfiles"),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media')
link showing me in django admin
media/media/ind_kQRDi6b.jpg
when i hit enter to open this this image it hit 127.0.0.1/8000/media/media/ind_kQRDi6b.jpg
And error show Page not found
Make sure that your url is correct. Like , http://127.0.0.1:8000/media/image.jpg
Related
I understand there are multiple (if not 100s) of questions pertaining to my issue. After trying all, I am here finally to ask. I am able to upload the image and the image path in the model. Example of an image field from model:
<ImageFieldFile: static/image1_FzGpiKx.jpeg>
My static folder is right where the project and app folders are. In similar hierarchy. I have the following settings in my settings.py file:
MEDIA_ROOT = '/static/'
MEDIA_URL = '/'
In my app level urls.py, here is what I have for rendering these images:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
After all this settings , my template shows the following as img src:
/static/image1_FzGpiKx.jpeg
Here is how I render in template:
<img src={{article.image.url}} />
Yet it just renders the typical broken image icon. Can someone help me here? Thank you!
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/static/'
use this in your settings file, and upload a new image and then check it
This worked fine everytime used to do django websites but this time it is giving me an error.
Settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'portfolio/static/')
]
STATIC_ROOT = os.path.join(BASE_DIR , 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
I have a profile.jpg in my directory Portfolio-Project/Portfolio/static/profile.jpg. It should collectstatic from here and paste the staticfiles in Portfolio-project/static as mentioned in my code. but it is giving ,me some error.
Error After using the command "Python manage.py collectstatic"
django.core.exceptions.SuspiciousFileOperation: The joined path
(C:\Users\Kiran\Desktop\portfolio-project\portfolio\static\Profile.jpg) is
located outside of the base path component
(C:\Users\Kiran\Desktop\portfolio- project\portfolio\static\)
Please Help.
Thanks
In your line:
os.path.join(BASE_DIR, 'portfolio/static/')
Delete the last slash:
os.path.join(BASE_DIR, 'portfolio/static')
Anyway, this is the ideal:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
I recently faced this error but it‘s actually simple:
Chances are you downloaded a Template, here is the solution:
First you have to check your css file for stuff like relative links
For example your CSS might be referencing a file outside your django project itself.
e.g. backround:url('...\image\Profile.jpg') this actually worked for me
Note:
The key fact is just to check your CSS or (maybe js) file first if it‘s a referencing file that you‘re not using or file that is referring to something that is not in your django project directory.
So I upload my site to digitalocean and when I went to the admin page the CSS was not showing
I visit all these sites but nothing seems to work
Django doc-static files
Pythonanywhere-DjangoStaticFiles
StackOverflow -why my django admin site does not have the css style
I set up my STATIC_ROOT and STATIC_URL, and then I ran
python manage.py collectstatic
And here is my seting
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/home/django/django_project/django_project/static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
You are specifying an Absolute Path for your join.
os.path.join(BASE_DIR, arg2) means join the current directory that is being executed and append the second argument.
add these lines into your settings.py file
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
I'm new to Django and Python.I want to display a link to my files stored in my static folder.Here is the required code from settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_URL = "/media/"
When I access these files through my admin I get the following url displayed in my browser:
http://127.0.0.1:8000/media/filename.pdf
Hence what I tried is to give a link to the file in my HTML code :
File
But instead of displaying http://127.0.0.1:8000/media/filename.pdf it just displays /media/filename.pdf which results in an error.
Adding localhost:8000/ before {{instance.docFile.url}} also didn't work.
Why isn't this working?What is the correct way to display link to my files?
Let me know if anything else is required.Thank you.
in your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in your settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/uploads/'
Then in your template
File
If you want know refer the docs Manage static files
The error is coming because your file is not stored in media.it is stored in static but your database store media URL. change your MEDIA_ROOT (given below) and try to upload one more time
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Don't put slash
:
File
Please see the Django setting MEDIA_ROOT documentation to better understand what your trying to do.
First MEDIA_ROOT and STATIC_ROOT must have different values.
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
See static files documentation for more details.
Second, You need to add these settings to your base urls.py.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If you want to use {{ MEDIA_URL }} in your templates, add 'django.template.context_processors.media' in the 'context_processors' option of TEMPLATES in settings.py.
At the top of your HTML page add {% load static %} then to display your media:
<img src="{{ MEDIA_URL }}{{ docFile }}" alt="test">
Please take your time with the Django Documentation, trust me it will help and also checkout the File upload settings
sorl - thumbnail image not displaying ...
after the long debug i found that cache folder is not created may be that's cause this issue ?
settings.py
INSTALLED_APPS = (
'sorl.thumbnail,
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
How to debug this ?
set in settings.py
THUMBNAIL_DEBUG = True
and see why it is not displaying.
you need to migrate sorl.thumbnail, so it will get its tables created, i think
You're missing ' at the end of 'sorl.thumbnail, in INSTALLED_APPS in your settings.py.