python - Django:图像/绝对 URL 不显示在 html 中

标签 python html django python-3.5 django-1.10

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

这是我的 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

非常感谢您抽出时间。 :)

最佳答案

我认为问题在于您根本没有模因列表的网址,因此您要么显示主视图,要么需要从 meme_list 中移动代码查看您的 home View ,或者您需要为 meme_list 创建一个 url(然后导航到该新 url)

关于python - Django:图像/绝对 URL 不显示在 html 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41672532/

相关文章:

python - 实现这种二维数值积分的计算速度更快的方法是什么?

html - 获得正确的导航和主要的左侧(不使用 float )

javascript - 使用 'background-size: contain' 在具有背景的 flexbox 中居中内容

javascript - 自动刷新特定的 div 博主 javascript

python - Django检查数据库中是否存在值,如果不存在则创建并保存

python - 在 python 中使用套接字发送文本和二进制文件

python - 来自 Think Python 的 has_no_e() 练习

django - 将 CSS 类添加到 django RadioSelect 标签

python - 按值python打印字典键和值

Python UnicodeEncodeError,但我已将参数编码为 UTF-8