- view.py 經過view,把展現頁面與數據庫關聯起來
from django.shortcuts import render
from models import Article
# Create your views here.
def home(request):
posts = Article.objects.all()
return render(request, 'home.html',{'posts':posts})
- templates
- 建立模板文件夾 "templates"
- 建立模板"index.html"
{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.pub_date }}</p>
<p>{{ post.content }}</p>
{% endfor%}
- urls.py
urlpatterns = [
...
url(r'^$', home),
...
]
- 注意事項 使用mysql可能會遇到亂碼
create database myBlog default charset=utf8;
python ./manage.py syncdb