当前位置: 首页 > news >正文

Django实现音乐网站 ⑿

使用Python Django框架制作一个音乐网站,

本篇主要是加载静态资源和推荐页-轮播图、推荐歌单功能开发。

目录

加载静态资源

引入jquery.js

引入bootstrap资源文件

创建基类模板样式文件

推荐页开发

轮播图开发

下载

加载swiper

自定义引入继承块设置

使用swiper

设置轮播div宽高

轮播图改活

视图查询轮播图表

模版循环遍历

推荐歌单

查询推荐歌单

模版显示推荐歌单

总结


加载静态资源

引入jquery.js

下载jquery.js文件到static/js目录中。

引入bootstrap资源文件

下载bootstrap资源包,从中取出css、js、font资源文件放置到static相应目录下。

如下图:

 

创建基类模板样式文件

在static/css下创建基类模板样式文件base.css;并在base.html中引用。

内容如下:

*{margin:0;padding:0;
}body {color:#3b3b3b;
}a {text-decoration:none;transition: none;border:none;
}a:hover {text-decoration:none;transition: none;
}li {list-style:none;
}#container {min-width: 1180px;max-width: 1640px;padding: 0 120px;margin: 0 auto;
}.header .logo{float:left;margin-right:14px;
}.header ul li{float:left;
}.header ul li a {display:block;height:69px;padding:0 14px;line-height:67px;font-size:20px;text-decoration: none;color:#3b3b3b;
}.header ul li a:hover{color:#000000;
}.header ul li a.selected{background-color:#ffe443;font-weight: bold;
}.clear {clear:both;
}.footer {width:100%;height:158px;background-color:#1f1f1f;text-align:center;color:#575757;padding-top:24px;margin-top: 62px;
}.swiper {width: 100%;height: 345px;margin-top:5px;
}.recommend_song_sheet {width:100%;height:400px;margin-top:70px;overflow:hidden;
}.recommend_song_sheet .title {height:40px;line-height:40px;margin-bottom:28px;overflow:hidden;
}.recommend_song_sheet .title .name{float:left;color:#000000;margin-right:20px;font-weight:bold;font-size: 28px;
}.recommend_song_sheet .title ul{float:left;
}.recommend_song_sheet .title ul li {float:left;
}.recommend_song_sheet .title ul li a{display:block;padding:0 20px;color:#3b3b3b;text-decoration: none;
}.recommend_song_sheet .title ul li a.now {font-weight:bold;text-shadow: #FC0 1px 0 10px;
}.recommend_song_sheet .list {width:100%;
}.recommend_song_sheet .list dl {width:263.9px;height:352px;float:left;margin-left:10px;cursor: pointer;
}.recommend_song_sheet .list dl.one {width:263.9px;height:352px;margin-left:0;
}.recommend_song_sheet .list dt {height:266px;
}.recommend_song_sheet .list dt img{width:263.9px;
}.recommend_song_sheet .list dd .name {margin-top: 16px;font-size: 16px;line-height: 22px;font-weight: 400;overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical;word-break: break-all;
}.recommend_song_sheet .list dd .count {margin-top: 10px;color: #999;
}.recommend_song_sheet .list dd .count i{font-size:14px;
}.recommend_rank {margin-top: 70px;
}.recommend_rank .title {height:40px;line-height:40px;margin-bottom:28px;overflow:hidden;
}.recommend_rank .title .name{float:left;color:#000000;margin-right:20px;font-weight:bold;font-size: 28px;
}.recommend_rank .title ul{float:left;
}.recommend_rank .title ul li {float:left;
}.recommend_rank .title ul li a{display:block;padding:0 20px;color:#3b3b3b;text-decoration: none;
}.recommend_rank .list {display: flex;justify-content: space-between;
}.recommend_rank .bank {width: 18.85%;box-shadow: 0 0 30px 0 rgba(65,67,70,.08);
}.recommend_rank .bank_top {position: relative;padding-bottom: 54.8%;overflow: hidden;
}.recommend_rank .bank_top .img {position: absolute;width: 100%;height: 100%;left: 0;top: 0;background: rgba(46,32,60,.7);z-index: 10;
}.recommend_rank .bank_top .img_tip{position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);width: 54.3%;z-index: 10;
}.recommend_rank .bank_top .img_bg {position: absolute;top: 50%;left: 0;width: 100%;transform: translateY(-50%);
}.recommend_rank .bank_list {padding: 28px 20px 26px;
}.recommend_rank .bank_list li {display: flex;justify-content: space-between;
}.recommend_rank .bank_list li .top_img {width: 20px;height: 33px;
}.recommend_rank .bank_list .top1 {background: url(/static/images/rank_1.png) center 4px no-repeat;background-size: auto;background-size: 168%;
}.recommend_rank .bank_list .top2 {background: url(/static/images/rank_2.png) center 4px no-repeat;background-size: auto;background-size: 168%;
}.recommend_rank .bank_list .top3 {background: url(/static/images/rank_3.png) center 4px no-repeat;background-size: auto;background-size: 168%;
}.recommend_rank .bank_list .top_index {padding-top: 3px;display: inline-block;font-weight: 600;font-size: 16px;width: 20px;text-align: center;
}.recommend_rank .bank_list .top_info {width: 85%;padding-left: 10px;
}.recommend_rank .bank_list .top_info .song_name:hover {font-weight: 600;
}.recommend_rank .bank_list .top_info .song_name {width: 100%;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;font-size: 16px;font-weight: 400;height: 22px;line-height: 22px;cursor: pointer;
}.recommend_rank .bank_list .top_info .singler:hover {color: #333;
}.recommend_rank .bank_list .top_info .singler {width: 100%;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;margin-top: 4px;font-size: 14px;font-weight: 300;color: #999;height: 20px;line-height: 20px;cursor: pointer;
}.recommend_singler {margin-top: 70px;
}.recommend_singler .title {height:40px;line-height:40px;margin-bottom:28px;overflow:hidden;
}.recommend_singler .title .name{float:left;color:#000000;margin-right:20px;font-weight:bold;font-size: 28px;
}.recommend_singler .title ul{float:left;
}.recommend_singler .title ul li {float:left;
}.recommend_singler .title ul li a{display:block;padding:0 20px;color:#3b3b3b;text-decoration: none;
}.recommend_singler .title ul li a.now {font-weight:bold;text-shadow: #FC0 1px 0 10px;
}.recommend_singler .list {display: flex;justify-content: space-between;
}.recommend_singler .list .item {width: 13%;text-align: center;
}.recommend_singler .list .item .cover {position: relative;width: 100%;padding-bottom: 100%;overflow: hidden;border-radius: 50%;font-size: 0;cursor: pointer;
}.recommend_singler .list .item .cover img{position: absolute;top: 0;left: 0;width: 100%;height: 100%;
}.recommend_singler .list .item .name {position: relative;height: 22px;font-size: 16px;line-height: 22px;font-weight: 400;margin-top: 17px;flex-wrap: nowrap;justify-content: center;padding: 0 5px;
}.recommend_singler .list .item .num {margin-top: 6px;font-size: 12px;color: #999;
}

 

推荐页开发

轮播图开发

使用swiper来处理轮播。

下载

下载Swiper - Swiper中文网

 

加载swiper

需要用到的文件有swiper-bundle.min.js和swiper-bundle.min.css文件。

解压后寻找swiper-bundle.min.js和swiper-bundle.min.css文件,

放置到myMusic/static相应资源目录下。

 

自定义引入继承块设置

在base.html中原有脚本文件引入位置之下设置一个引入js文件继承块,

用来让子页面来自定义继承脚本文件。

内容如下:

<script src="{% static 'js/jquery-3.7.0.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
{% block styleJs %}
{# 子页面引入js文件 #}
{% endblock styleJs %}

使用swiper

在index.html页面引入css和js文件,找两个图片放入images目录中。

先做一个轮播效果,之后改为调用后台数据。

内容如下:

{% extends 'common/base.html' %}
{% load static %}{% block title %}我的音乐{% endblock title %}{% block content %}
<link rel="stylesheet" href="{% static 'css/swiper-bundle.min.css' %}"><div class="swiper"><div class="swiper-wrapper"><div class="swiper-slide"><img src="{% static 'images/1.jpg' %}" alt=""></div><div class="swiper-slide"><img src="{% static 'images/2.jpg' %}" alt=""></div>
</div><!-- 分页器 --><div class="swiper-pagination"></div><!-- 导航按钮 --><div class="swiper-button-prev"></div><div class="swiper-button-next"></div>
</div>
<!--导航等组件可以放在Swiper容器之外-->
{% endblock content %}{% block styleJs %}
{# 子页面引入js文件 #}
<script src="{% static 'js/swiper-bundle.min.js' %}"></script>
<script>var mySwiper = new Swiper ('.swiper', {loop: true, // 循环模式选项// 分页器pagination: {el: '.swiper-pagination',clickable: true,// 设置分页器点击切换},// 前进后退按钮navigation: {nextEl: '.swiper-button-next',prevEl: '.swiper-button-prev',},// 自动切换autoplay: {delay: 3000,disableOnInteraction: false,},})
</script>
{% endblock styleJs %}

 

设置轮播div宽高

在base.css中设置轮播图所在div的宽高。

内容如下:

.swiper {width: 100%;height: 345px;margin-top:5px;
}

效果:

 

轮播图改活

轮播图改为后台提供数据。

视图查询轮播图表

路由之前已经设置,只需要查询轮播图数据并返回给模版。

代码如下:

from django.shortcuts import render
from .models import Carouseldef index(request):""" 显示首页 """# 获取首页轮播图carousel_imgs = Carousel.objects.all()return render(request, 'index/index.html', {'carousels': carousel_imgs})

 

模版循环遍历

把原来前端写死的轮播图改为后端返回数据,通过for循环遍历并加上跳转。

代码如下:

<div class="swiper"><div class="swiper-wrapper">{% for ca in carousels %}<div class="swiper-slide"><a href="{{ ca.href }}"><img src="/media/{{ ca.path }}" alt=""></a></div>{% endfor %}</div><!-- 分页器 --><div class="swiper-pagination"></div><!-- 导航按钮 --><div class="swiper-button-prev"></div><div class="swiper-button-next"></div>
</div>

推荐歌单

模版已经做好,现在需要改为后台提供数据。

这里先确认规则为播放量最多的五个返回给前端。

因为目前播放量增加功能还没做,先把数据表中修改一下播放量。

查询推荐歌单

还是在视图中首页方法中处理;需要排序和分页设置。

代码如下:

from django.shortcuts import render
from .models import Carousel, SongSheetdef index(request):""" 显示首页 """# 获取首页轮播图carousel_imgs = Carousel.objects.all()# 推荐歌单# 选播放量最多的五个songsheets = SongSheet.objects.order_by('-playnum').all()[0:5]return render(request, 'index/index.html', {'carousels': carousel_imgs, 'songsheets': songsheets})

模版显示推荐歌单

因为目前还没做歌单详情功能,现在歌单类型和歌单详情跳转先不做;

当前先做首页推荐的显示。

本来想使用enumerate函数取得对象的key属性判断第一个赋值元素属性,结果发现在模版中解析不了;突然想起来循环中forloop.counter属性;这里主要使用了循环和在循环中进行条件判断。

代码如下:

<!--推荐歌单开始-->
<div class="recommend_song_sheet"><div class="title"><div class="name">推荐歌单</div><ul><li><a href="#" class="now">每日推荐</a></li><li><a href="#">翻唱</a></li><li><a href="#">网络</a></li><li><a href="#">伤感</a></li><li><a href="#">欧美</a></li><li><a href="#">更多></a></li></ul></div><div class="list">{% for son in songsheets %}{% if forloop.counter == 1 %}<dl class="one"><dt><img src="/media/{{ son.cover }}" alt=""></dt><dd><p class="name">{{ son.name }}</p><p class="count"><i class="glyphicon glyphicon-play"></i> {{ son.playnum }}</p></dd></dl>{% else %}<dl><dt><img src="/media/{{ son.cover }}" alt=""></dt><dd><p class="name">{{ son.name }}</p><p class="count"><i class="glyphicon glyphicon-play"></i> {{ son.playnum }}</p></dd></dl>{% endif %}{% endfor %}</div>
</div>
<!--推荐歌单结束-->

效果:

总结

目前开发比较顺利,费时的主要是前端的样式设置,感觉自己的技术还是偏向于后端方面。

只能是多加练习了。

相关文章:

Django实现音乐网站 ⑿

使用Python Django框架制作一个音乐网站&#xff0c; 本篇主要是加载静态资源和推荐页-轮播图、推荐歌单功能开发。 目录 加载静态资源 引入jquery.js 引入bootstrap资源文件 创建基类模板样式文件 推荐页开发 轮播图开发 下载 加载swiper 自定义引入继承块设置 使用…...

ORB-SLAM2学习笔记10之图像关键帧KeyFrame

文章目录 0 引言1 KeyFrame类1.1 构造函数1.2 成员函数1.3 关键帧之间共视图1.3.1 AddConnection1.3.2 UpdateBestCovisibles1.3.3 UpdateConnections1.3.4 EraseConnection1.3.5 SetBadFlag 1.4 地图点1.5 生成树 2 KeyFrame用途 0 引言 ORB-SLAM2学习笔记7详细了解了System主…...

【ownCloud】添加信任域

在我进行使用mysql:5.6和 owncloud 镜像&#xff0c;构建一个个人网盘后 我的虚拟机更改了ip地址导致出现下列状况 报错&#xff1a;您正在访问来自不信任域名的服务器。 please contact your administrator. if you are an administrator of this instance, configure the &q…...

C++--类型转换

1.什么是类型转换 在传统C语言中&#xff0c;由强制类型转换和隐式类型转换&#xff0c;隐式类型转换&#xff0c;编译器在在编译阶段自动处理&#xff0c;能转换则转换&#xff0c;强制类型转换由用户自己转换。 缺陷&#xff1a; 转换的可视性比较差&#xff0c;所有的转换形…...

在服务器上部署 Nginx 并设置图片服务器

问题&#xff1a;我要指定/home/images专门存放图片&#xff01;该怎么做&#xff0c;而且我的系统是centos8系统&#xff0c;只有一个root用户&#xff0c;用root用户已经安装了nginx 答案&#xff1a; 既然你使用了 CentOS 8&#xff0c;并且你想使用 /home/images 目录存放…...

使用NXP GUI GUIDER生成的GUI移植到雅特力MCU平台过程详解(ST/GD/国民/极海通用)

好记性不如烂笔头&#xff0c;既然不够聪明&#xff0c;就乖乖的做笔记&#xff0c;温故而知新。 本文档用于本人对知识点的梳理和记录 一、前言 上一篇我们有介绍NXP GUI Guider工具如何制作和调试GUI&#xff0c;GUI神器 NXP GUI GUIDER开发工具入门教程https://blog.csdn.n…...

JVM——配置常用参数,GC调优策略

文章目录 JVM 配置常用参数Java内存区域常见配置参数概览堆参数回收器参数项目中常用配置常用组合 常用 GC 调优策略GC 调优原则GC 调优目的GC 调优策略 JVM 配置常用参数 Java内存区域常见配置参数概览堆参数&#xff1b;回收器参数&#xff1b;项目中常用配置&#xff1b;常…...

使用IDEA把Java程序打包成jar

点击左上角File,选择Project Structure 左侧选中Artifacts,点击右侧的号 选择JAR->From modules with dependencies 选择你要运行的main方法所在的类,选好了点击OK Artifacts添加完成后点击右下角OK 在工具栏中找到Build,选择Build Artifacts 刚才创建好的Artifacts,选择Bui…...

元宇宙和数字孪生的异同探究

元宇宙和数字孪生&#xff0c;作为两个备受瞩目的概念&#xff0c;都在不同领域引起了巨大的关注。虽然它们都涉及数字化世界的构建&#xff0c;但元宇宙和数字孪生在概念、应用和影响方面存在一些异同点。 相似之处&#xff1a; 数字表示&#xff1a; 元宇宙和数字孪生都依赖…...

初识微服务

我们在曾经最常见的就是所谓的单体架构&#xff0c;但是由于网民越来越多&#xff0c;单体架构已经逐渐的被淘汰出去&#xff0c;所以我们在单体架构的基础上提出了微服务&#xff0c;它提倡将单一应用程序划分成一组小的服务&#xff0c;服务之间互相协调、互相配合&#xff0…...

数据库锁的分类 各种锁

锁的一个分类 数据库中的锁前言分享链接个人总结全局锁&#xff1a;表级锁行级锁&#xff1a; 数据库中的锁 前言 C支持并发有锁&#xff0c;Linux里面也有锁机制&#xff0c;数据库也有锁&#xff0c;什么互斥锁&#xff0c;表级锁&#xff0c;间隙锁&#xff0c;好多…&…...

MySQL数据库软件

MySQL数据库软件的详细知识介绍: 1. 存储引擎 MySQL支持多种存储引擎,如InnoDB、MyISAM等。不同引擎有各自的特点,InnoDB支持事务、行锁,MyISAM支持全文索引等。 2. 索引结构 MySQL索引主要有B树索引、哈希索引、全文索引等。这些索引通过不同的数据结构加速查找效率。 3. …...

无涯教程-PHP - preg_match_all()函数

preg_match_all() - 语法 int preg_match_all (string pattern, string string, array pattern_array [, int order]); preg_match_all()函数匹配字符串中所有出现的模式。 它将按照您使用可选输入参数order指定的顺序将这些匹配项放置在pattern_array数组中。有两种可能的类…...

Docker 练习2 安装MySQL

一、实验要求 1、使用mysql:5.6和 owncloud 镜像&#xff0c;构建一个个人网盘。 2、安装搭建私有仓库 Harbor 3、编写Dockerfile制作Web应用系统nginx镜像&#xff0c;生成镜像nginx:v1.1&#xff0c;并推送其到私有仓库。具体要求如下&#xff1a; &#xff08;1&#xff09…...

AndroidStudio 编译报错Unable to make field private final

用 AndroidStudio 打开某个工程后&#xff0c;编译报错如下 Execution failed for task :app:processDebugMainManifest. > Unable to make field private final java.lang.String java.io.File.path accessible: module java.base does not "opens java.io" to …...

linux 上安装es

首先 到官网 https://www.elastic.co/cn/downloads/elasticsearch 下载对应的安装包&#xff0c;我这里下载的是 https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.9.1-linux-x86_64.tar.gz 然后讲该压缩包上传到 linux 的/usr/local 目录下执行 tar -z…...

自然语言处理从入门到应用——LangChain:索引(Indexes)-[文本分割器(Text Splitters)]

分类目录&#xff1a;《自然语言处理从入门到应用》总目录 当我们需要处理长文本时&#xff0c;有必要将文本分割成块。虽然这听起来很简单&#xff0c;但这里存在很多潜在的复杂性。理想情况下&#xff0c;我们希望将语义相关的文本块保持在一起&#xff0c;但什么是"语义…...

Chrome如何安装插件(文件夹)

1.下载的插件 说明&#xff1a;插件文件夹 2.打开扩展程序位置 3.点击已加载的扩展程序 说明&#xff1a;找到插件的位置 4.报错 说明&#xff1a;那还要进入文件里面。 5.插件的位置 说明&#xff1a;如果已经安装了插件&#xff0c;那么需要查看插件的位置。chrome输入 …...

MySql 环境搭建

目录 MySql 在 CentOS 7 环境下安装。 说明&#xff1a; 1.卸载不要的环境 2.配置 mysql 官方 yum 源 3.开始安装 4.启动 mysql 5.mysql 登录 6.配置 mysql 7. 设置开机启动 MySql 在 CentOS 7 环境下安装。 说明&#xff1a; 在安装与卸载中&#xff0c;用户切换成 r…...

mysql 表的约束

目录 mysql 表的约束 NULL/NOT NULL DEFAULT comment zerofill PRIMARY KRY 删除主键 添加主键 复合主键 AUTO_INCREMENT UNIQUE KEY FOREIGN KEY mysql 表的约束 约束是 mysql 为了保证数据正确的一种手段&#xff0c;而前面在谈数据类型的时候&#xff0c;数据类…...

wordpress后台更新后 前端没变化的解决方法

使用siteground主机的wordpress网站&#xff0c;会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后&#xff0c;网站没有变化的情况。 不熟悉siteground主机的新手&#xff0c;遇到这个问题&#xff0c;就很抓狂&#xff0c;明明是哪都没操作错误&#x…...

电脑插入多块移动硬盘后经常出现卡顿和蓝屏

当电脑在插入多块移动硬盘后频繁出现卡顿和蓝屏问题时&#xff0c;可能涉及硬件资源冲突、驱动兼容性、供电不足或系统设置等多方面原因。以下是逐步排查和解决方案&#xff1a; 1. 检查电源供电问题 问题原因&#xff1a;多块移动硬盘同时运行可能导致USB接口供电不足&#x…...

P3 QT项目----记事本(3.8)

3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

SpringTask-03.入门案例

一.入门案例 启动类&#xff1a; package com.sky;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCach…...

并发编程 - go版

1.并发编程基础概念 进程和线程 A. 进程是程序在操作系统中的一次执行过程&#xff0c;系统进行资源分配和调度的一个独立单位。B. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。C.一个进程可以创建和撤销多个线程;同一个进程中…...

Python网页自动化Selenium中文文档

1. 安装 1.1. 安装 Selenium Python bindings 提供了一个简单的API&#xff0c;让你使用Selenium WebDriver来编写功能/校验测试。 通过Selenium Python的API&#xff0c;你可以非常直观的使用Selenium WebDriver的所有功能。 Selenium Python bindings 使用非常简洁方便的A…...

鸿蒙(HarmonyOS5)实现跳一跳小游戏

下面我将介绍如何使用鸿蒙的ArkUI框架&#xff0c;实现一个简单的跳一跳小游戏。 1. 项目结构 src/main/ets/ ├── MainAbility │ ├── pages │ │ ├── Index.ets // 主页面 │ │ └── GamePage.ets // 游戏页面 │ └── model │ …...

Neko虚拟浏览器远程协作方案:Docker+内网穿透技术部署实践

前言&#xff1a;本文将向开发者介绍一款创新性协作工具——Neko虚拟浏览器。在数字化协作场景中&#xff0c;跨地域的团队常需面对实时共享屏幕、协同编辑文档等需求。通过本指南&#xff0c;你将掌握在Ubuntu系统中使用容器化技术部署该工具的具体方案&#xff0c;并结合内网…...

表单设计器拖拽对象时添加属性

背景&#xff1a;因为项目需要。自写设计器。遇到的坑在此记录 使用的拖拽组件时vuedraggable。下面放上局部示例截图。 坑1。draggable标签在拖拽时可以获取到被拖拽的对象属性定义 要使用 :clone, 而不是clone。我想应该是因为draggable标签比较特。另外在使用**:clone时要将…...