2
I’m having a hard time with my Django project. I have a template to upload the images, where I have a field to add a description for each one, and another to search all the images with this description.
Upload the images to the media folder, which is set to MEDIA_ROOT in my Settings.py
MEDIA_ROOT = (
os.path.join(BASE_DIR, 'media') #pasta media para abrigar os arquivos dos usuários
)
MEDIA_URL is also configured as follows:
MEDIA_URL = '/media/'
Image upload template: (service order is the description I’m referring to)
class Document(models.Model):
Ordem_de_serviço = models.ForeignKey(model_os, on_delete=models.CASCADE)
Foto = models.ImageField(upload_to='media')
uploaded_at = models.DateTimeField(auto_now_add=True)
That’s where the problem is: when I upload_to 'media', it creates a new folder called media within the existing media folder. when I leave it blank, it uploads to the project root! And the links get broken in the visualization template in both ways, the only way I can view the images in the template is by copying into the media folder.
Remembering that I also added the following line to my.py urls:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And this in the Processors context, within TEMPLATES in Settings.py
'django.template.context_processors.media'
Thanks for your help.
EDIT
My Urls.py:
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from expedicao import views
from expedicao.views import ListaOrdemServico, PesquisaView, HomePageView, SearchResultsView, CriaOrdemServico, model_form_upload, AtualizaOrdemServico
urlpatterns = [
path('admin/', admin.site.urls),
path('', HomePageView.as_view(), name='home'),
path('list/', ListaOrdemServico.as_view(), name='lista_ordem_servico'),
path('new/', CriaOrdemServico.as_view(), name='ordem_servico_form'),
path('<int:pk>/edit/', AtualizaOrdemServico.as_view(), name='ordem_servico_edit'),
url(r'^uploads/form/$', views.model_form_upload, name='model_form_upload'),
path('search/', PesquisaView.as_view(), name='search'),
path('searchresults', SearchResultsView.as_view(), name='search_results'),
] += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I did, but unfortunately it didn’t. Now you’re pointing syntax error when I try to run the server. By the way, I did not post the code of the full urls.py in the question, this line I entered in the question is at the end of it. I’ll edit the question and put the full code.
– Alex Afonso dos Santos
Must edit to put this line before setting the paths/urls?
– Alex Afonso dos Santos
I edited the answer, try again.
– Victor Fernandes
I managed to solve the Upload problem, it is no longer creating the new folder and is saving the images in the media folder, just as I wanted. But the link still looks for the image at the root. By copying the link it returns the following : http://192.168. 2.108:8000/Jellyfish_6ti6dgg.jpg (IP is different from the default localhost because I’m also running some tests on mobile)
– Alex Afonso dos Santos
As for this, just add ". url" at the end of the image output, for example: Object.imagem.url
– Victor Fernandes
That’s right! Now it worked! Thank you very much!
– Alex Afonso dos Santos