Anaconda+django写出第一个web app(十一)
今天我们来学习给页面添加一个Sidebar,根据Sidebar跳转到相应的tutorial。
打开views.py,编辑single_slug函数:
def single_slug(request, single_slug):
categories = [c.category_slug for c in TutorialCategory.objects.all()]
if single_slug in categories:
matching_series = TutorialSeries.objects.filter(tutorial_category__category_slug=single_slug)
series_urls = {} for m in matching_series.all():
part_one = Tutorial.objects.filter(tutorial_series__tutorial_series=m.tutorial_series).earliest('tutorial_published')
series_urls[m] = part_one.tutorial_slug return render(request=request,
template_name='main/category.html',
context={'tutorial_series':matching_series, 'part_ones': series_urls}) tutorials = [t.tutorial_slug for t in Tutorial.objects.all()]
if single_slug in tutorials:
this_tutorial = Tutorial.objects.get(tutorial_slug=single_slug)
tutorials_from_series = Tutorial.objects.filter(tutorial_series__tutorial_series=this_tutorial.tutorial_series).order_by('tutorial_published')
this_tutorial_idx = list(tutorials_from_series).index(this_tutorial)
return render(request=request,
template_name='main/tutorial.html',
context={'tutorial':this_tutorial, 'sidebar':tutorials_from_series, 'this_tut_idx':this_tutorial_idx})
接下来我们需要编写tutorial.html,来显示相应的内容和Sidebar,Sidebar的写法参考materialize:
{% extends 'main/header.html' %}
{% block content %}
<div class="row">
<div class="col s12, m8, l8">
<h3>{{tutorial.tutorial_title}}</h3>
<p style="font-size: 70%">Published {{tutorial.tutorial_published}}</p>
{{tutorial.tutorial_content|safe}}
</div>
<div class="col s12 m4 l4">
<ul class="collapsible popout">
{% for tutorial in sidebar %}
{% if forloop.counter0 == this_tut_idx %}
<li class="active">
<div class="collapsible-header" style="background-color: #008E72; color: white;">{{tutorial.tutorial_title}}<br>(currently viewing)</div>
</li>
{% else %}
<li>
<div class="collapsible-header">{{tutorial.tutorial_title}}</div>
<div class="collapsible-body">
<p><a href="/{{tutorial.tutorial_slug}}"><button class="btn waves-effect waves-light right-align" style="background-color:green-light; color:white">Go</button></a></p>
</div>
</li>
{% endif %}
{% endfor %}
</ul>
</div>
</div>
{% endblock %}
我上传了几个Ted视频,效果显示如下,这时点击Sidebar没有反应,我们还需要在header.html中初始化javascript,在最后一行写上:
<script>M.AutoInit();</script> ,Sidebar即可正常使用:

点击想要跳转的tutorial,点击go,即可跳转。

到此为止,我们的第一个app已经建成了,这个web app可以显示目录的类别、系列以及查看每个目录的内容。如果我们想要增加更多的功能,也非常的方便,接下来还需要学习html、数据库、python的知识,来一步步提高,多查阅帮助文档和看看喜欢的网站,然后想想如何实现,亲自动手操作一遍。
目前为止使用的都是python默认的数据库sqlite3,下一节继续对这个网站学习如何把sqlite数据库转化为mysql数据库。
参考链接:
[1] https://materializecss.com/collapsible.html
[2] https://pythonprogramming.net/dynamic-sidebar-django-tutorial/
Anaconda+django写出第一个web app(十一)的更多相关文章
- Anaconda+django写出第一个web app(一)
在安装好Anaconda和django之后,我们就可以开始创建自己的第一个Web app,那么首先创建一个空文件夹,之后创建的文件都在这个文件夹内. 启动命令行进入此文件夹内,可以先通过如下命令查看一 ...
- Anaconda+django写出第一个web app(十)
今天继续学习外键的使用. 当我们有了category.series和很多tutorials时,我们查看某个tutorial,可能需要这样的路径http://127.0.0.1:8000/categor ...
- Anaconda+django写出第一个web app(九)
今天来学习外键的使用,用外键来连接数据库中的两个表. 当我们的tutorials非常多的时候,目前的显示方式就会使得页面非常凌乱.我们可以考虑把这些教程分为不同的系列,页面只显示标题以及概要等信息,进 ...
- Anaconda+django写出第一个web app(三)
前面我们已经建立了模型Tutorial,也已经可以用Navicat Premium打开数据看查看数据,接下来我们通过建立admin账户来上传数据. 在命令行执行如下命令来创建用户: python ma ...
- Anaconda+django写出第一个web app(二)
今天开始建立App中的第一个Model,命名为Tutorial. Model的定义在main文件夹下的models.py中通过类进行,我们希望Tutorial这个model包含三个属性:标题.内容和发 ...
- Anaconda+django写出第一个web app(七)
今天来实现如何在页面弹出一些信息,比如注册成功后弹出注册成功的信息.这一点可以通过materialize里的Toasts来实现. django自带的messages可以告诉我们是否注册成功,以及注册失 ...
- Anaconda+django写出第一个web app(六)
今天学习如何写一个注册用户的界面. 上一节的导航栏中我们修改了导航栏右侧的文字为register并将路径设置为/register,内容如下: <li><a href="/r ...
- Anaconda+django写出第一个web app(四)
前面对Models有了一些了解,今天开始进一步了解Views,了解Views如何和Models交互以及了解模板(templates). 打开main文件夹下的views.py,重新编写homepage ...
- Anaconda+django写出第一个web app(八)
今天来实现网站的登入和登出功能. 首先我们需要在urls.py中添加路径,注意此处的路径和在导航栏中设置的文字路径保持一致: from django.urls import path from . i ...
随机推荐
- PHPMyWind5.4存储XSS后续getshell提权
0x0 前言 通过留言处的xss,我们可以得到管理员的cookie,进而登陆后台: https://www.cnblogs.com/Rain99-/p/10701769.html 现在要从后台入手,进 ...
- linux内核分析第一次实验
http://blog.sina.com.cn/s/blog_78e559950102wneg.html
- github优缺点
以前bitbucket沒有支援git github可以直接在網站上瀏覽push的圖片 github 可以針對code行數直接留言與回覆 github Markdown支援很好 github 的issu ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- SqlServer测试SQL语句执行效率
方法一: SET STATISTICS io ON SET STATISTICS time ON go ---需要测试的sql语句 go SET STATISTICS profile OFF SET ...
- (转)linux 内存管理——内核的shmall 和shmmax 参数
内核的 shmall 和 shmmax 参数 SHMMAX= 配置了最大的内存segment的大小 ------>这个设置的比SGA_MAX_SIZE大比较好. SHMMIN= 最小的内存seg ...
- 爆打团队 四则运算 beta视频
爆打团队 四则运算 beta视频链接 http://v.youku.com/v_show/id_XMTU1MjAzNDI0NA==.html?from=s1.8-1-1.2
- CentOS下安装Python3
目录 CentOS下安装Python3 下载 解压 配置 gcc sudo权限 vim 编译 安装 添加软链接 pip安装出错,找不到SSL 安装virtualenv和virtualenvwrappe ...
- Pycharm+Python+PyQt5使用
https://www.cnblogs.com/dalanjing/p/6978373.html
- JAVA AES CBC PKCS5Padding加解密
package com.hzxc.groupactivity.util; /** * Created by hdwang on 2019/1/17. */ import org.slf4j.Logge ...