Django:Images/Absolute URL 未在 html 中显示
Django: Images/Absolute URLS from not displaying in html
我的模板上有一段代码没有显示在我的 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
非常感谢您抽出宝贵时间。 :)
我认为问题在于您根本没有用于模因列表的 url,因此要么您正在显示主视图,要么需要从 meme_list
查看您的 home
视图,或者您需要为 meme_list
创建一个 url(然后导航到新的 url)
我的模板上有一段代码没有显示在我的 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
非常感谢您抽出宝贵时间。 :)
我认为问题在于您根本没有用于模因列表的 url,因此要么您正在显示主视图,要么需要从 meme_list
查看您的 home
视图,或者您需要为 meme_list
创建一个 url(然后导航到新的 url)