📅  最后修改于: 2023-12-03 14:58:13.617000             🧑  作者: Mango
该主题旨在介绍铃声下载相关的信息。
现如今,很多人喜欢将自己喜欢的音乐、声音、音效等作为手机铃声来警示自己有来电或短信,让自己赏心悦目。而铃声下载应运而生,为用户提供方便、快捷、免费的铃声下载服务。这个应用程序的目的在于为用户提供多种类型的铃声下载,适配各种智能手机,并提供实时铃声推荐,让用户轻松找到自己喜爱的铃声。
from django.shortcuts import render
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Ringtones
from .serializers import RingtonesSerializer
@api_view(['GET', 'POST'])
def ringtone_list(request):
"""
List all ringtones or create a new ringtone
"""
if request.method == 'GET':
ringtones = Ringtones.objects.all()
serializer = RingtonesSerializer(ringtones, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = RingtonesSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>铃声下载</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
</head>
<body>
<div id="app">
<h1 class="text-center">铃声下载</h1>
<hr>
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="search">搜索铃声</label>
<input type="text" class="form-control" v-model="search" v-on:keyup.enter="searchRingtone()">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">下载铃声</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item" v-for="(ringtone, index) in ringtones" :key="ringtone.id">
<div class="media-body">
{{ ringtone.name }}
<a :href="ringtone.url" class="btn btn-success pull-right">下载</a>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">推荐铃声</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item" v-for="(ringtone, index) in recommendRingtones" :key="ringtone.id">
{{ index+1 }}.{{ ringtone.name }}
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
search: '',
ringtones: [],
recommendRingtones: []
},
methods: {
searchRingtone: function(){
this.$http.get('/api/ringtones/?search='+this.search).then(response => {
this.ringtones = response.body;
}, response => {
console.log('Error');
});
},
getRecommendRingtones: function(){
this.$http.get('/api/ringtones/get_recommend/').then(response => {
this.recommendRingtones = response.body;
}, response => {
console.log('Error');
});
}
},
mounted() {
this.getRecommendRingtones()
}
});
</script>
</body>
</html>
以上为后台接口代码片段和前端页面代码片段,后端和前端交互数据使用RESTful API实现。