1

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. :)

The_Pythonist
  • 101
  • 10
  • 1
    Your question is not clear. Are you saying that the entire "container-fluid" div is not showing? – Daniel Roseman Jan 16 '17 at 09:02
  • No. I'm saying that the python code block in my HTML is not working (after

    popular

    )
    – The_Pythonist Jan 16 '17 at 09:06
  • 1
    For your future sanity, please consider renaming the model to something friendlier than `m`... Are you sure your database has entries in it? – Sayse Jan 16 '17 at 09:06
  • yes it does have entries and I can view them if I type in hostname/m/2 explicitly on my URL bar where 2 is an id number – The_Pythonist Jan 16 '17 at 09:08
  • As far as I can see you don't have a URL that points to `meme_list`, which is the only view that passes `object_list`. How are you expecting to use that view? The index view renders the same template but without any context. – Daniel Roseman Jan 16 '17 at 09:09
  • I see .Is there anyway I can put two views in one URL as they both would be rendering parts of the same HTML file. – The_Pythonist Jan 16 '17 at 09:14
  • 2
    No, but *you don't need that* and they are not rendering "parts of the same HTML file". A view renders the *whole* template. Move the code into the index view. – Daniel Roseman Jan 16 '17 at 09:18
  • Thank you so much for pointing that out, I will do that when I'm back at work. – The_Pythonist Jan 16 '17 at 09:20

1 Answers1

2

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)

Sayse
  • 42,633
  • 14
  • 77
  • 146