Pergunta

We are making our school site on Django, we made a gallery app but i think the images are not getting uploaded to designated folder, rather they are not even getting uploaded i think so . Code : -

# views.py
def upload(request, template_name="gallery/form.html"):
   form = GalleryForm(request.POST or None, request.FILES or None)
   if form.is_valid():
     form.save()
     messages.success(request, "image has been uploaded")
     return redirect('gallery.views.index')

   return render(request, template_name,{'form':form})

from django.db import models

# models.py
class GalleryPhoto(models.Model):
   image = models.ImageField(upload_to='pic_folder/')
   description = models.CharField(max_length=50)

   def __unicode__(self):
      return self.image.url

The rest of the code can be seen in this repo
Note : the the gallery template contains some conflicts but that is not the actual problem.

Foi útil?

Solução

It took me a while to notice it, because it's kinda tricky. But in retrospect it's obvious.

So after configuring MEDIA_URL = "/MEDIA/" I noticed that http://127.0.0.1:8000/media/ was giving me a 404. If everything is configured correctly (and it is) then usually the problem is conflicting urls. See the url patterns used in django are always evaluated in order. That means if you have urls like so:

url(r'^mypage/', 'myapp.mypage'),
url(r'^mypage/another/', 'myapp.different')

Then the second url will never be available. Why? because there's nothing up there that tells the url dispatcher that the first url should end at mypage/. As far as it's concerned, everything starting with http://DOMAIN/mypage/ will be caught and passed to the first function.

To avoid that, we use the $ sign to tell the dispatcher at what point to stop. Like so:

url(r'^mypage/$', 'myapp.mypage'),
url(r'^mypage/another/$', 'myapp.different')

Now let's look at your last two urls:

    url(r'^', include('pages.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

See that? url(r'^' swallows everything that comes after it, so no media is being served. It's true that pages.urls uses a dollar for its first url:

url(r'^$', views.index, name='index'),

but that doesn't matter for the main url dispatcher, which evaluates r'^' first. What's the solution? the simplest would be to move the pages urls to the end:

    url(r'^ckeditor/', include('ckeditor.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += patterns('', url(r'^', include('pages.urls')))

You can also isolate that one problematic url from the others using a direct call, like so:

url(r'^/$', 'pages.views.index'),
url(r'^pages/$',  include('pages.urls')),

After that, this will work:

<img src="{{ MEDIA_URL }}{{ image.image.url }}"  alt="{{ image.description }}">

p.s.

Eventually you're going to pass the media and static files through your server, when you move to deployment level, so maybe the whole thing wouldn't matter then. If you do decide to keep the r'^' in use, just keep in mind that it might cause conflicting problems with other urls, so be sure to always put it last.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top