效果
建立学习笔记网页。其中包括主页和话题,话题中又有具体的条目。有条目则显示,没有则提示。
urls.py views.py html
base.html index.html topics.html topic.html
主页
分类
某分类下具体条目
无条目
urls.py learning_log/
1 from django.conf.urls import urls, include 2 from django.contrib import admin # 3 4 urlpatterns=[ 5 url(r'^admin/', admin.site.urls) 6 url(r'', include('learning_logs.urls', namespace='learning_logs')), 7 ] PPhsoa
urls.py learning_logs/
1 from django.conf.urls import url 2 from . import views 3 4 urlpatterns=[ 5 url(r'^$', views.index, name='index'), 6 url(r'^topics/$', views.topics, name='topics'), 7 url(r'^topics/(?P<topic.id>d+)/$', views.topic, name='topic'), 8 ]
views.py learning_logs/
1 from django.shortcuts import render # 2 from .models import Topic 3 4 def index(request): 5 return render(request, 'learning_logs/index.html') 6 7 def topics(request): 8 topics = Topic.objects.order_by('date_added') 9 context = {'topics':topics} 10 return render(request, 'learning_logs/topics.html', context) 11 12 def topic(request, topic_id): 13 topic = Topic.objects.get(id=topic_id) 14 entries = topic.entry_set.order_by('-date_added') 15 context = {'topic':topic, 'entries':entries} 16 return render(request, 'learning_logs.topic.html', context)
base.html / learning_log/ learning_logs/templates/learning_logs/
1 <p> 2 <a href="{% url 'learning_logs:index' %}">Learning Log</a>- 3 <a hrer="{% url 'learning_logs:topics' %}">Topics</a> 4 </p> 5 6 {% block content %} 7 {% endblock content %}
index.html / learning_log/ learning_logs/templates/learning_logs/
1 {% extends 'learning_logs/base.html' %} 2 3 {% block content %} 4 <p>Learning log helps you learn better..</p> 5 {% endblock content %}
topics.html / learning_log/ learning_logs/templates/learning_logs/
1 {% extends 'learning_logs/bse.html' %} 2 3 {% block content %} 4 <p>Topics</p> 5 <ul> 6 {% for topic in topics %} 7 <li> 8 <a href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a> 9 </li> 10 {% empty %} 11 No topic has been added here. 12 {% endfor %} 13 </ul> 14 {% endblock content %}
topic.html / learning_log/ learning_logs/templates/learning_logs/
1 {% extends 'learning_logs/base.html' %} 2 3 {% block content %} 4 <p>Topic:{{ topic }}</p> 5 <p>Entries:</p> 6 <ul> 7 {% for entry in entries %} 8 <li> 9 <p>{{ entry.date_added|date:'M d, Y H:i' }}</p> 10 <p>{{ entry.text|linebreaks }}</p> 11 </li> 12 {% empty %} 13 There are no entries for this topic yet. 14 {% endfor %} 15 </ul> 16 {% endblock content %}