1.模板for循环
2.博客页面
{% for xx in xx %}
html
{% endfor %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h1>文章列表</h1>
{% for article in articles %}
<a href="">{{article.title}}</a>
</br>
{% endfor %}
</body>
</html>
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from . import models
def hello(request):
#return HttpResponse('<html>HELLO WORLD!</html>')
articles = models.Article.objects.all()
return render(request, 'Blog/index.html', {'articles': articles})
2.博客页面
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from . import models
def hello(request):
#return HttpResponse('<html>HELLO WORLD!</html>')
articles = models.Article.objects.all()
return render(request, 'Blog/index.html', {'articles': articles})
def article_page(request,article_id):
article = models.Article.objects.get(pk=article_id)
return render(request, 'Blog/article_page.html', {'article': article})
from django.conf.urls import url,include
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.hello),
url(r'^article/(?P<article_id>[0-9]+)', views.article_page)
]
3.超链接配置
在根目录urls.py中配置namespace,下级目录urls.py中配置name
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h1>文章列表</h1>
{% for article in articles %}
<a href="{% url 'Blog:article_page' article.id %}">{{article.title}}</a>
</br>
{% endfor %}
</body>
</html>