Django 1.9 - Not displaying user uploaded images -


i trying display images have been uploaded user, no matter try getting broken link icon. have searched , searched through documentation, on , elsewhere couple of days no avail. new django , in development i'm sure i've made other rookie mistakes, right thing care displaying uploaded images in templates.

here relevant snippets of code:

settings.py

media_url = '/media/media_root/' media_root = os.path.join(os.path.dirname(base_dir), "media", "media_root") 

urls.py

urlpatterns = [     url(r'^admin/?', admin.site.urls),     url(r'^accounts/', include('registration.backends.simple.urls')),     url(r'^about/?', profiles.views.about, name='about'),     url(r'^properties/single/?', properties.views.single, name='single_mens'),     url(r'^properties/married/?', properties.views.married, name='married'),     url(r'^properties/add/add_photos/?', properties.views.add_photos, name='add_photos'),     url(r'^properties/add/?', properties.views.add_rental, name='add_rental'),     url(r'^', profiles.views.home, name='home'), ]   urlpatterns += static(settings.static_url, document_root=settings.static_root) urlpatterns += static(settings.media_url, document_root=settings.media_root) urlpatterns += staticfiles_urlpatterns() 

models.py

class rentalpicmodel(models.model):      def __unicode__(self):         return self.image.url      image = models.imagefield(upload_to="pics/originals/", null=true)     rental = models.foreignkey(rentalmodel, on_delete=models.cascade) 

forms.py

class addphotosform(forms.modelform):      class meta:         model = rentalpicmodel         fields = ['image', 'rental']      def clean_image(self):         return self.cleaned_data['image']      def clean_rental(self):         return self.cleaned_data['rental'] 

views.py

def add_photos(request):      form = addphotosform     current_rental = none     current_photos = []     if request.method == "post":         form = addphotosform(request.post, request.files)         if request.post.get('another'):             if form.is_valid():                 cleaned_image = form.cleaned_data['image']                 cleaned_rental = form.cleaned_data['rental']                 current_rental = cleaned_rental                  pic = rentalpicmodel(image=cleaned_image, rental=cleaned_rental)                 pic.save()          current_photos = rentalpicmodel.objects.filter(rental=current_rental)         current_photos = [rental.image rental in current_photos]         photo in current_photos:             print photo      context = {         'form' : form,         'photos' : current_photos,     }      return render(request, "add_photos.html", context) 

here output of print statement (after uploading 1 photo) is: pics/originals/dsc_1376.jpg , can see file saved location.

add_photos.html

    <div class="container">         <h1>upload photos here.</h1>         <br>         <div class='row'>             <form method="post" action="" enctype="multipart/form-data"> {% csrf_token %}                 {{ form|crispy }}                 <div class='col col-xs-3'></div>                 <div class='col col-xs-3'>                     <input class="btn btn-block btn-info" name="another" type="submit" value="save , add another">                 </div>                 <div class='col col-xs-3'>                     <input class="btn btn-block btn-primary" name="finish" type="submit" value="save , finish">                 </div>                 <div class="col col-xs-3"></div>             </form>         </div>          {% if photos|length > 0 %}             <h2>uploaded photos:</h2>             {% photo in photos %}                 <div class='row'>                     <img src="{{ photo.url }}" alt="">                 </div>             {% endfor %}         {% endif %}     </div> 

when inspect <img> element, see src="/media/media_root/pics/originals/dsc_1376.jpg" gives me url of http://127.0.0.1:8000/media/media_root/pics/originals/dsc_1376.jpg. seems correct file location me, still not displaying.

like say, seems me set how described in django documentation , in every other question i've read on so. missing?

thank in advance.

edit

do need modify staticfiles_dirs setting @ uploaded media? here have right now:

staticfiles_dirs = (     os.path.join(base_dir, "static_files"), ) 

which i've put css , javascript files.

my urls problem. when trying retrieve media files, matched url(r'^', profiles.views.home, name='home') before matched of media urls. simple $ fixed it:

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

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -