Django:图像/绝对URLS不显示在HTML中

时间:2017-01-16 08:54:07

标签: python html django python-3.5 django-1.10

我的模板上有一段代码,它没有显示在我的html上,也没有在Chromes控制台上给出任何错误。我正在尝试显示一个图像列表,单击该图像会将您带到该图像的详细信息。

以下是 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

非常感谢你的时间。 :)

1 个答案:

答案 0 :(得分:2)

我认为问题在于你没有meme列表的url,所以你要么显示主视图并需要将代码从meme_list视图移动到你的home视图,或者您需要为meme_list创建一个网址(并导航到该新网址)