Search code examples
pythonhtmldjangopython-3.5django-1.10

Django: Images/Absolute URLS from not displaying in html


I have a block of code on my template which does not show up on my html nor does it give any error on Chromes console. I am trying to display a list of images which when clicked takes you to the detail of that image.

Here is the key part of my HTML(base.html):

 <div class="container-fluid">
        <h2>Popular</h2> #only this shows up
        {% for obj in object_list %}
        <img src = "{{ obj.mpost.url}}"  width="300"><br>
        <a href='/m/{{ obj.id }}/'> {{obj.username}} </a><br/>
        {% endfor %}
    </div>

views.py:

from django.shortcuts import render,get_object_or_404
from .models import m

# Create your views here.
def home(request):
    return render(request,"base.html",{})

def m_detail(request,id=None):
    instance = get_object_or_404(m,id=id)
    context = {
        "mpost": instance.mpost,
        "instance": instance
    }
    return render(request,"m_detail.html",context)

def meme_list(request): #list items not showing
    queryset = m.objects.all()
    context = {
        "object_list": queryset,
    }
    return render(request, "base.html", context)

urls.py:

urlpatterns = [
    url(r'^$', home, name='home'),
    url(r'^m/(?P<id>\d+)/$', m_detail, name='detail'),#issue or not?
]

models.py:

class m(models.Model):
    username = "anonymous"
    mpost = models.ImageField(upload_to='anon_m')
    creation_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return m.username

Thank you so much for your time. :)


Solution

  • I assume the problem is the fact that you don't have a url at all for the meme list, so either you're showing the home view and need to move the code from the meme_list view into your home view, or you need to make a url for the meme_list (and navigate to that new url instead)