Python音乐平台Django实战(三)

从零开始的Django框架入门到实战教程(内含实战实例) - 01 创建项目与app、加入静态文件、模板语法介绍(学习笔记)   在这部分,我将从Django的安装讲起,从创建项目到制作一个简单的页面(跑通流程),再到加入网页,介绍Django的模板语法。最后利用所学完成一个小的网页展示。   1. 安装Django 2. 创建项目(startproject) 2.1 创建项目 2.2 介绍项目文件 2.3 APP(startapp) 3. 走通流程(urls, views, runserver) 3.1 第一个页面 3.2 多个页面 4. 网页 4.1 加入网页(templates, render) 4.2加入图片、css、js 阅读详情

1 定义路由列表

项目中设置了6个项目应用,每个项目应用实现不同的网页功能,在开发网页功能之前,首先为各个项目应用设置路由空间,再由各个项目应用的urls.py定义具体的路由信息。打开music的urls.py定义项目的路由列表,在路由列表中定义各个项目应用的路由空间。

代码如下:

from django.contrib import admin
from django.urls import path, re_path, include
# serve() 视图可以用来为你给它的任何目录提供服务。
from django.views.static import serve
from django.conf import settings
​
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('index.urls')),
    path('ranking.html', include('ranking.urls')),
    path('play/', include('play.urls')),
    path('comment/', include('comment.urls')),
    path('search/', include('search.urls')),
    path('user/', include('user.urls')),
    # 定义静态资源的路由信息
    re_path('static/(?P<path>.*)', serve, {'document_root': settings.STATIC_ROOT}, name='static'),
    # 定义媒体资源的路由信息
    re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
]

上述的路由空间以最简单的方式定义,因为项目的网页数量不多,所以路由空间可以无须设置参数namespace。

由于项目的配置文件settings.py设置媒体资源文件夹media,因此还需要在路由对象urlpatterns中设置媒体资源的路由信息。

2 编写公用模板

在模板文件夹templates中编写公用模板文件base.html,该文件用于定义整个音乐网站平台的网页结构。打开公用模板文件base.html,编写以下网页代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title>我的音乐</title>
    {% block link %}{% endblock %}
</head>
<body>
    {% block body %}{% endblock %}
    <div class="footer">
    <div class="copyright">
        <p>网站数据信息来源于网络</p>
    </div>
    </div>
</body>
</html>

模板文件base.html定义了模板继承接口link和body,每个接口负责实现不同的功能。接口link用于引入css样式文件、JavaScript脚本文件等静态文件;接口body用于编写网页内容。

3 网站首页

网站首页是整个网站的主页面,从网站的需求设计来看,首页一共实现7个功能:歌曲搜索、轮播图、音乐分类、热门歌曲、新歌推荐、热门搜索个热门下载。

网站首页在index中实现,首先在index的urls.py中定义路由index,路由的HTTP请求由视图函数indexView接收和处理,路由index的定义如下所示:

from django.urls import path
from .views import *
​
urlpatterns = [
    path('', indexView, name='index'),
]

下一步在index的views.py中定义视图函数indexView,视图函数indexView主要实现模型Dynamic、Label和Song的数据查询。

代码实现如下:

from django.shortcuts import render
from .models import *
​
​
def indexView(request):
    songDynamic = Dynamic.objects.select_related('song')
    # 热搜歌曲
    searchs = songDynamic.order_by('-search').all()[:8]
    # 音乐分类
    labels = Label.objects.all()
    # 热门歌曲
    popular = songDynamic.order_by('-plays').all()[:10]
    # 新歌推荐
    recommend = Song.objects.order_by('-release').all()[:3]
    # 热门搜索、热门下载
    downloads = songDynamic
基于Python与Django的音乐播放器开发实战 随着互联网技术的发展,音乐播放器已从单一的音频播放工具演变为集播放、搜索、推荐于一体的多媒体应用。Python凭借其简洁易读的语法、丰富的第三方库和强大的Web开发能力,成为开发音乐播放器的理想语言。本章将介绍如何利用Python及其Django框架构建后端服务,配合前端技术实现一个功能完整的音乐播放器。通过本项目,开发者可以深入理解前后端协同开发流程、数据库建模、API对接、音频播放控制等关键技术点,为构建更复杂的Web应用打下坚实基础。接下来,我们将上述表结构转化为 Django 的模型类。 阅读详情

相关推荐

Aveva Marine C# 二次开发入门001

1# 引用 C:\AVEVA\Marine\OH12.1.SP4\Aveva.ApplicationFramework.dll C:\AVEVA\Marine\OH12.1.SP4\Aveva.ApplicationFramework.Presentation.dll 2# 引用命名空间, using Aveva.ApplicationFramework.Presentation;usi...

weixin_30245867的博客 1912

常见的十大量化投资策略(附源码)

量化投资策略是利用量化的方法,进行金融市场的分析、判断和交易的策略、算法的总称。著名的量化投资策略有以下10种(注:策略源码模板不能直接用于实盘交易,仅供探讨交流) 01、海龟交易策略 海龟交易策略是一套非常完整的趋势跟随型的自动化交易策略。这个复杂的策略在入场条件、仓位控制、资金管理、止损止盈等各个环节,都进行了详细的设计,这基本上可以作为复杂交易策略设计和开发的模板。 海龟交易策略源码:https://www.myquant.cn/docs/python_strategyies/110 0.

小壁虎的春天 2万+

Fluent新手必看:如何正确解读scaled residuals曲线(附常见问题排查)

本文详细解析了Fluent中scaled residuals曲线的解读方法及常见问题排查技巧。针对continuity残差居高不下、湍流模型收敛困难等典型问题,提供了网格质量检查、边界条件验证和求解策略调整的实用解决方案,帮助CFD新手快速掌握计算收敛性的关键判断标准。

weixin_28698059的博客 379

HFSS螺旋线圈的设计与仿真

undefined.全局变量在这里进行设置:Project/Project Properties, browse and setup the global variables.undefined.方程中的局部变量设置:HFSS/Design Properties, browse and setup the local variables。按照下图填写曲线的方程内容:fill the parameters according to the setup in the following picture。

johnboat的专栏 3847

工程制图习题集张大庆课后习题答案解析

所以,我想向大家求助,希望能得到一些学习资料、学习方法和学习经验,帮助我更好地理解和掌握这门课程。在寻找学习资料的过程中,我发现了一款叫做资小料的APP,它里面有很多关于工程制图的学习资料,包括视频教程、习题解答和考试真题等。通过使用资小料,我逐渐掌握了工程制图的基本理论和方法,解决了很多学习中遇到的问题。在这里,我想推荐给和我一样在学习《工程制图习题集》这门课的同学们,希望大家也能从这款APP中受益,共同进步。上传者: 『Consentesdei』蒼迷穀。出版社: 清华大学出版社。

weixin_58833001的博客 927

DSH-Work:一个让DeepSeek下载即用的Harness客户端

使用大模型工具时,环境配置往往比模型本身更让人头疼。DeepSeek Harness作为一类封装层,将模型调用、上下文管理、参数控制等复杂细节统一收拢,对外提供简洁操作入口。开源的DSH-Work正是这样一款Harness客户端,它通过自带运行时与保守默认配置,实现了下载即用、免配环境的目标,让使用者无需掌握Python、CUDA等专业知识即可快速连接DeepSeek云端或本地服务。对于非技术背景的用户,它降低了AI工具的使用门槛;对于开发者,它把时间从环境排错中解放出来,更专注于提示词调优与工作流设计。无

weixin_29048309的博客 347

关于 ABAP OPEN SQL 的动态 WHERE 条件和 Dynamic Selections

ABAP动态查询技术解析 本文介绍了ABAP中实现灵活数据库查询的动态选择技术,通过FREE_SELECTIONS函数组实现用户自定义查询条件。核心内容包括: 传统固定WHERE条件的局限性,无法满足业务多变需求 Dynamic Selections机制的工作原理,使用FREE_SELECTIONS_INIT初始化、FREE_SELECTIONS_DIALOG交互 完整Demo程序演示如何构建通用表数据浏览器: 用户输入表名后校验权限 调用动态选择函数生成条件 动态拼接WHERE子句执行查询 使用CL_SA

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 678

Python在振动信号处理中的应用(一):振动信号消除多项式趋势项

文章目录一、概述二、最小二乘法去除趋势项原理三、python模块介绍3.1 scikitlearn简介(1)转化器(Transformer)(2)估计器(Estimator)3.2 线性回归LinearRegression3.3 多项式PolynomialFeatures3.4 工作流Pipeline四、python代码实现4.1 构造模拟信号4.2 编写去除趋势项主代码4.3 展示算法效果五、TIPS 一、概述   在振动测试过程中,传感器或采集设备由于自身性能问题或受环境干扰(如温度、供电等影响),容易

白银时代的博客 8579

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

上一篇: Python音乐平台Django实战(二)
下一篇: Python音乐平台Django实战——歌曲搜索
Python_P叔
博客等级 码龄3年 5454粉丝 827原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值