打开learning_log中的文件urls.py 能够将learning_logs的URL同项目中其他URL区分开
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('learning_logs.urls', namespace='learning_logs')),
]
在learning_logs中创建另一个urls.py
"""Defines url patterns for learning_logs."""
from django.conf.urls import url
from . import views
urlpatterns = [
# Home page.
url(r'^$', views.index, name='index'),
]
url有三个参数,第一个为一个正则表达式,这里表示没有任何东西,第二个参数为要调用的视图函数,第三个参数将这个URL
模式的名称定位index.Python忽略项目的基础URL(http://localhost:8000/,因此这个表示的就是用户访问基础URL, 会调用view.index返回页面
编写视图views.py
URL请求与我们刚才定义的模式匹配时,Django将在文件views.py中查找函数index(),再将请求对象传递个这个视图函数。
from django.shortcuts import render
from .models import Topic
def index(request):
"""The home page for Learning Log."""
return render(request, 'learning_logs/index.html')
编写模板index.html
Learning Log helps you keep track of your learning, for any topic you're
learning about.
创建网站时,几乎都有一些所有网页都将包含的元素,我们可以编写一个通用元素的父模板,并让每个网页都继承这个模板。
在index.html同一个文件夹下创建base.html
{% block content %}{% endbock content %}
第二行插入了一个名为content的{% block %}标签。不是从父模板继承的内容都包含在content模块中
修改index.html
{% extends "learning_logs/base.html" %}
{% block content %}
Learning Log helps you keep track of your learning, for any topic you're
learning about.
{% endblock content %}
修改learning_logs/urls.py
"""Defines url patterns for learning_logs."""
from django.conf.urls import url
from . import views
urlpatterns = [
# Home page.
url(r'^$', views.index, name='index'),
# 显示所有的主题
url(r'^topics/$', views.topics, name="topics"),
]
修改视图views.py
函数topics()需要从数据库中获取一些数据,将其发送给模板。
from django.shortcuts import render
from .models import Topic
def index(request):
"""The home page for Learning Log."""
return render(request, 'learning_logs/index.html')
def topics(request):
"""显示所有的主题"""
topics = Topic.objects.order_by('data_added')
context = {'topics' : topics}
return render(request, 'learning_logs/topics.html', context)
Django从服务器那里收到的request对象,我们查询数据库,并将结果保存到topics变量中,context是一个要发送给模板的
上下文,键为访问数据的名称,值为发送给模板的数据。
模板topics.html
同样的创建topics.html
首先使用{% extends %}继承base.html,然后定义content块,使用一个相当于for循环的模板标签,它遍历字典context中的列表topics.{% endfor %}表示for循环结束标签. {{topic}}都被替换为topic的当前值。
{% extends "learning_logs/base.html" %}
{% block content %}
{% for topic in topics %}
- {{topic}}
{% empty %}
- No topics have been added yet.
{% endfor %}
{% endblock content %}
修改base.html 使其包含到显示所有主题的页面的链接
{% block content %}{% endbock content %}
显示特定主题的页面
URL模式
显示特定主题的页面的URL模式与前面的所有URL模式稍有不同,因为它将使用主题的id属性来指出请求是哪个主题.
如果用户要查看主题Chess(其id为1)的详细页面,URL将为http://localhost:8000/topics/1/
修改urls.py
url(r'^topics/(?P
/d就是匹配任意一个数字,和[0-9]是一样的,+表示匹配一个字符一次或无数次
?P
当发现URL与这个模式匹配时,Django将调用视图函数topic(),并将存储在topic_id的值作为实参传递给它。
"""Defines url patterns for learning_logs."""
from django.conf.urls import url
from . import views
urlpatterns = [
# Home page.
url(r'^$', views.index, name='index'),
# 显示所有的主题
url(r'^topics/$', views.topics, name="topics"),
# 特定主题的详细页面
url(r'^topics/(?P\d+)/$', views.topic, name="topic"),
]
from django.shortcuts import render
from .models import Topic
def index(request):
"""The home page for Learning Log."""
return render(request, 'learning_logs/index.html')
def topics(request):
"""显示所有的主题"""
topics = Topic.objects.order_by('data_added')
context = {'topics' : topics}
return render(request, 'learning_logs/topics.html', context)
def topic(request, topic_id):
"""显示单个主题及其所有的条目"""
topic = Topic.objects.get(id = topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topics' : topics, 'entries' : entries}
return render(request, 'learning_logs/topic.html', context)
{% extends 'learning_logs/base.html' %}
{% block content %}
Topic: {{topic}}
Entries:
{% for entry in entries %}
-
{{ entry.date_added|date:'M d, Y H:i' }}
{{ entry.text|linebreaks }}
{% empty %}
-
There are no entries for this topic yet.
{% endfor %}
{% endblock content %}
{% extends "learning_logs/base.html" %}
{% block content %}
Topics
{% for topic in topics %}
-
{{topic}}
{% empty %}
- No topics have been added yet.
{% endfor %}
{% endblock content %}
然后访问http://localhost:8000/topics/1/