3👍
✅
Pass the list of urls to the template and use the {% for %}
tag to loop them.
urls.py
urlpatterns = patterns('',
url(r'^$', 'myapp.views.top_urls', name='home'),
url(r'^admin/', include(admin.site.urls)),
)
views.py
def top_urls(request):
p = MyHTMLParser()
p.feed(urllib2.urlopen('http://www.alexa.com/topsites/global').read())
urls = p.site_list[:20]
print urls
return render(request, 'top_urls.html', {'urls': urls})
top_urls.html
...
<tbody>
{% for url in urls %}
<tr>
<td>Something</td>
<td>{{ url }}</td>
<td>something</td>
</tr>
{% endfor %}
</tbody>
...
Source:stackexchange.com