简体   繁体   中英

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

我认为问题是这样的事实,即您根本没有用于meme列表的url,因此您要么显示主视图,要么需要将代码从meme_list视图移到home视图,或者您需要为meme_list创建一个URL(并导航到该新URL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM