Reputation: 17
im following a django tutorial on how to make a blog, and we are at the template tags, the thing is, only the head is showing up and not the articles that i placed into my template, this is my code:
views
from django.shortcuts import render
from.models import Narticle
def narticle_list(request):
narticle= Narticle.objects.all().order_by('date')
return render(request,'narticle/narticle_list', {'narticle': narticle})
template narticle_list
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Narticle</title>
</head>
<body>
<h1>narticle_list</h1>
<div class="narticle">
<h2> <a href="#">{{Narticle.title}}</a> </h2>
<p>{{Narticle.body}}</p>
<p>{{Narticle.date}}</p>
</div>
</body>
</html>
in case you want to see my urls
from django.conf.urls import url, include
from django.contrib import admin
from. import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^narticle/', include ('narticle.urls')),
url(r'^about/$', views.about),
url(r'^$',views.homepage),
url for narticle
from django.conf.urls import url
from. import views
urlpatterns = [
url(r'^$',views.narticle_list),
]
when i request the narticle url, my articles are not showing up, just the header which is "narticle_list"
Upvotes: 1
Views: 173
Reputation: 2558
You are passing a collection into the context of the template. This collection acts like a python list, so you need to iterate over it. You can do this with template logic:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Narticle</title>
</head>
<body>
<h1>narticle_list</h1>
{% for a in narticle %}
<div class="narticle">
<h2> <a href="#">{{a.title}}</a> </h2>
<p>{{a.body}}</p>
<p>{{a.date}}</p>
</div>
{% endfor %}
</body>
</html>
To clarify, the collection is what you get from Narticle.objects.all().order_by('date')
. You refer to the narticle
from the template context in the {% for a in narticle %}
line. Be sure to close the loop with {% endfor %}
. You can access properties or attributes as you have already done in your example with dot notation. Everything else looks like it should work.
Upvotes: 2