Django Admin: Determine correct image upload path
<p>I am new to Django and I am currently having problems in showing uploaded images in Django Admin. I have followed many posted Q and A here in stackoverflow but of those worked in my problem. I hope any active of the coding ninja here could help me with this problem. Here is the detailed view of the problem:</p>

<ol>
<li><p>I have defined the MEDIA_ROOT and MEDIA_URL in <em>settings.py</em></p>

<pre><code>BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
</code></pre></li>
<li><p>This is my upload model in <em>models.py</em>:</p>

<pre><code>from django.utils.html import mark_safe

class ImageDetails(models.Model):
    image = models.ImageField(null=True)    

    def image_img(self):
        if self.image:
            return mark_safe('&lt;img src="%s" height="125px" width="125px"/&gt;' % (self.image.url))
        else:
            return '(No image found)'
    image_img.short_description = 'Thumbnail'
</code></pre></li>
<li><p>In my Application <em>urls.py</em>:</p>

<pre><code>urlpatterns = [
    url(r'^inputImage', views.inputImage, name='inputImage'),
    url(r'', views.index),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
</code></pre></li>
<li><p>In my admin.py:</p>

<pre><code>class ImageDetailsAdmin(admin.ModelAdmin):
    fields = ["image"]  #for file upload
    list_display = ("image_img",)

admin.site.register(ImageDetails, ImageDetailsAdmin)
</code></pre></li>
</ol>

<p>The image was successfully stored at <em>ProjectDIR/media</em>. The HTML returns the url: <a href="http://127.0.0.1:8000/media/imagename.jpg" rel="nofollow">http://127.0.0.1:8000/media/imagename.jpg</a> at img tag. But the page fails to load the image (I will be redirected to index page whenever when using the url <a href="http://127.0.0.1:8000/media/imagename.jpg" rel="nofollow">http://127.0.0.1:8000/media/imagename.jpg</a>). I am using Django version 1.10</p>

<p>As suspected, this problem is about URLs in Django. The problem occurred because I declared the following urlpattern in an app urls.py:</p>

<pre><code>     url(r'', views.index, name='index'),
</code></pre>

<p>I solved the problem by changing the code to:</p>

<pre><code>     url(r'^$', views.index, name='index'),
</code></pre>

<p>And adding the following code at the project's main urls.py:</p>

<pre><code>    if settings.DEBUG:
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 
</code></pre>

python django django-admin django-urls django-1.10