I am trying to save images from my Form to my database in django. All of my other CharFields seem to save from my form but not the images. When I go to admin I can upload and save images to a specified media folder and it works however, the images dont seem to save. Please help very confused, also new to this so my code may be a mess, much help is needed. Thanks. Links to my code below:
[updated models][1]
[updated settings][2]
[updated urls][3]
[updated views.py][1]
links to code:
[1]: https://i.stack.imgur.com/Td7Mv.png
[2]: https://i.stack.imgur.com/31vNB.png
[3]: https://i.stack.imgur.com/BXhg7.png
[4]: https://i.stack.imgur.com/fu5BC.png
In your models.py file you need to change all your ImageField to this
media_gallery = models.ImageField(blank=True, null=True, upload_to='photos/%Y/%m/%d/')
This saves the location of the image in the database
Later you can access the image in templates like this
src = "{{ model_name.media_gallery.url }}"
After everything lastly add this code to your settings.py file
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Please update your main project's urls.py file by adding this code at the end
urlpatterns = [] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Related
My Django app import an image from the media_url and then it goes to the next page and shows the image and goes back to first page then a new image imported and the old image is removed.
The problem is that at the first loop everything is good, in the second loop, it appears that html shows the old image not the new one. how can I apply a reloading in html. here is part of the code:
settings.py:
MEDIA_ROOT = 'media//' # I test the MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media//'
views.py:
def scraping(request):
....
template_name = "scraping.html"
response2 = {'media_root': Config.MEDIA_ROOT, 'media_url': Config.MEDIA_URL}
return render(request, template_name, response2)
scraping.html
<img src='{{media_url}}/pic.jpg//'> # I also tried {{media_url}}pic.jpg
{{media_url}}/pic.jpg/
{{media_url}}//pic.jpg//
You can try the code below :
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')
Well, I didn't got any answer yet, but I realize something. first I changed the media directory to static.
here it is:
scraping.html
<img src='{{static_url}}/' \>
<img src='{{static_url}}//' \>
<img src='{{static_url}}///' \>
each time I have to add a "/" in src to reload my image as it is right now.
if I delete one "/" after refreshing it goes to previous image.
I played a lot in settings and views and html by replacing "/" and "\" but I don't have any clue why its happening
Well, I didn't find any answer for my question, but I solved my problem bypassing this issue.
In the previous version, I tried to import an image , rename it into pic.jpg and call it on the html file. this caused the web to show me all the pre-removed pic.jpg.
Now, I rename all the imported image to a specific name (by date.now()) and this helped html not to get confused by the similar name of the files.
But still I don't know why the html file wouldn't refresh the image even if the name of the source files are the same.
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
A part of my website allows users to upload pictures and videos, just like facebook or instagram, a user can upload either video or picture and it will be rendered on the template depending on the file extension type. Whatever the user uploads either picture or video should be rendered on the template. Am using Django.
models.py
views.py
index.html
First You Need To Join Your Media directory to your app
Here is the Way You Can Do That:
First You Need To Add This in Your Setting.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASEDIR,'your folder directory')
for example app/media or something
Then You Need To Create Media Url Here is The Way:
Then You Need To Add in your root and app urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('your_url_link', views.your_view_name,name='your_url_name'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Then You Need To Add This in Your Html File
<img src='{{ context.Image.url }}'>
#Context mean your returning object Dictionary In Image Add Your Model Image Field Name
You Can See More Details Here:
https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.fields.files.FieldFile.url
I am using Django 1.5.4 and I have a project with the following structure:
django_site
-django_site
-books # the app
-media
-images
-books
-authors
-static
-images
-templates
This is the necessary code:
# settings.py
MEDIA_ROOT = '/root/django_site/books/media/'
MEDIA_URL = '/media/'
# models.py
class Book(models.Model):
image = models.ImageField(upload_to="images/books/")
# urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^books/$', 'books.views.get_all_books'),
)
My problem is that, after adding the image via the admin site, if I click on the image link below the upload button a 404 page shows up, complaining the path does not exist.
The request URL is http://127.0.0.1:8000/media/books/media/images/book/out_of_control_1.JPG but in fact I want the result to be /root/django_site/books/media/images/out_of_control_1.JPG.
How do I fix that? Looking forward to your responses.
Since you seem to use the developpement server, I think your issue is related to this one : Django MEDIA_URL and MEDIA_ROOT
As explained in the answer, you have to set up a specific URL pattern for media handling when debug is set to True.
You can also have a look at the docs regarding this question.
I am having some problems with serving user uploaded files from my Django application:
from models.py:
class Picture (models.Model):
title = models.CharField(max_length=48)
date_added = models.DateTimeField(auto_now=True)
content = models.ImageField(upload_to='pictures')
From the Django admin the files get uploaded to the user_res/pictures/ folder.
from the project's settings.py:
MEDIA_ROOT = 'user_res'
MEDIA_URL = '/user_res/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
Every time I try to reference a static resource (namely css or js files), everything works fine using URLs such as
http://localhost:8000/static/<subfolder>/main.css.
However, I cannot access user uploaded files (which get created by the admin interface in the user_res/pictures folder with a relative URL such as
user_res/pictures/test.jpg
the URL is dynamically created with this line of code from a Django Picture model callable:
return '<img src="{}"/>'.format(self.content.url)
I have no dedicated url-s for either static or media files in the url.py file.
Does anybody have any idea as to how to make Django serve the media files? I understand that for live environments I will need to configure an http server to serve that particular directory, but for now I want to maintain a lightweight development suite.
Thank you.
Edit your urls.py file as shown below.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
edit your projects settings.py to look like:
#Rest of the settings
MEDIA_URL = '/media/'
MEDIA_ROOT = 'media'
STATIC_ROOT = ''
STATIC_URL = '/static/'
Please read the official Django documentation about serving files uploaded by a user carefully. Link to docs: https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user
I think the url attribute returns a relative URL ( Django's FileField documentation ), so you should have:
return '<img src="{}"/>'.format(MEDIA_URL + self.content.url)
Relative URLs won't work, as a user visiting "http://localhost/books/" would be requesting "http://localhost/books/user_res/pictures/test.jpg".