Simple Basketball Information Management System with Django
This article presents a simple basketball information management system built with Django, detailing CRUD operations for player data, pagination logic, and corresponding HTML templates for both admin and front‑end interfaces, accompanied by full source code snippets and screenshots of the resulting pages.
The author shares a simple basketball information management system and invites feedback from fellow developers.
Features: The system implements create, read, update, and delete (CRUD) operations for player information and displays the data on a front‑end page.
Main code: The following Django view handles player queries, keyword search, pagination, and renders the admin template.
<code><span><span>def</span> <span>qiuyuan</span>(<span>request</span>):</span>
<span> qiuyuans = models.qiuyuan.objects.<span>all</span>()</span>
<span> pn=request.GET.get(<span>'pn'</span>,<span>1</span>)</span>
<span> <span>try</span>:</span>
<span> pn=<span>int</span>(pn)</span>
<span> <span>except</span>:</span>
<span> pn=<span>1</span></span>
<span> <span>#搜索</span></span>
<span> keyword=request.GET.get(<span>'keyword'</span>,<span>None</span>)</span>
<span> <span>if</span> keyword <span>is</span> <span>not</span> <span>None</span>:</span>
<span> qiuyuans=models.qiuyuan.objects.<span>filter</span>(xingming__icontains=keyword).<span>all</span>()</span>
<span> <span>else</span>:</span>
<span> qiuyuans=models.qiuyuan.objects.<span>all</span>()</span>
<span> <span>#分页,分上一页和下一页</span></span>
<span> paginator=Paginator(qiuyuans,<span>5</span>) <span>#a1:查询结果集 a2:每页显示记录数</span></span>
<span> <span>try</span>:</span>
<span> qiuyuans=paginator.page(pn) <span>#获取某一页记录</span></span>
<span> <span>except</span> (EmptyPage,InvalidPage,PageNotAnInteger) <span>as</span> e:</span>
<span> pn=<span>1</span></span>
<span> qiuyuans=paginator.page(pn)</span>
<span> <span>#获取总页数</span></span>
<span> num_pages=qiuyuans.paginator.num_pages</span>
<span> <span>#分页数字显示</span></span>
<span> <span>#显示5个数字,当前页数放在中间(高亮显示)</span></span>
<span> <span>if</span> num_pages>=<span>5</span>: <span>#总页数大于你想要的显示的分页数字</span></span>
<span> <span>if</span> pn<=<span>2</span>:</span>
<span> start=<span>1</span></span>
<span> end=<span>6</span></span>
<span> <span>elif</span> pn>num_pages-<span>2</span>:</span>
<span> start=num_pages-<span>1</span></span>
<span> end=num_pages+<span>1</span></span>
<span> <span>else</span>:</span>
<span> start=pn-<span>2</span></span>
<span> end=pn+<span>3</span></span>
<span> <span>else</span>:</span>
<span> start=<span>1</span></span>
<span> end=num_pages+<span>1</span></span>
<span> numbers=<span>range</span>(start,end)</span>
<span> context = {</span>
<span> <span>'qiuyuan'</span>: <span>'active'</span>,</span>
<span> <span>'qiuyuans'</span>: qiuyuans,</span>
<span> <span>'num_pages'</span>:num_pages,</span>
<span> <span>'numbers'</span>:numbers,</span>
<span> <span>'pn'</span>:pn,</span>
<span> }</span>
<span> <span>return</span> render(request,<span>'houtai/qiuyuan.html'</span>,context)</span></code>Add information template: This Django template extends a base layout and renders a table of player fields with actions for deletion and modification, plus a link to add new records.
<code><span>{% extends 'houtai/base.html' %}</span>
<span>{% block main %}</span>
<span> <span><table class="table table-bordered" method="post"></span></span>
<span> <span><tr></span></span>
<span> <span><th>ID</th></span></span>
<span> <span><th>球员姓名</th></span></span>
<span> <span><th>球员国籍</th></span></span>
<span> <span><th>球衣号码</th></span></span>
<span> <span><th>场上位置</th></span></span>
<span> <span><th>球员年龄</th></span></span>
<span> <span><th>球员身高</th></span></span>
<span> <span><th>球员体重</th></span></span>
<span> <span><th>所在俱乐部</th></span></span>
<span> <span><th>操作</th></span></span>
<span> <span></tr></span></span>
<span> {% for qy in qiuyuans %}</span>
<span> <span><tr></span></span>
<span> <span><td>{{ qy.id }}</td></span></span>
<span> <span><td>{{ qy.xingming }}</td></span></span>
<span> <span><td>{{ qy.guoji }}</td></span></span>
<span> <span><td>{{ qy.haoma }}</td></span></span>
<span> <span><td>{{ qy.weizhi }}</td></span></span>
<span> <span><td>{{ qy.nianling }}</td></span></span>
<span> <span><td>{{ qy.shenggao }}</td></span></span>
<span> <span><td>{{ qy.tizhong }}</td></span></span>
<span> <span><td>{{ qy.jlb.mingcheng }}</td></span></span>
<span> <span><td></span>
<span> <span><a href="{% url 'houtai:qy_del' %}?id={{qy.id}}" onclick="return confirm('你确定要删除吗')">删除</a></span>
<span> <span><a href="{% url 'qy_edit' %}?id={{qy.id}}">修改</a></span>
<span> <span></td></span>
<span> <span></tr></span></span>
<span> {% endfor %}</span>
<span> <span></table></span></span>
<span> <span><tr></span></span>
<span> <span><submit><a href="{% url 'qy_add' %}">添加</a></submit></span></span>
<span> <span></tr></span></span>
<span> {% include 'houtai/qy_page.html' %}</span>
<span>{% endblock %}</span></code>Edit information template: This form allows updating a player's details, including name, nationality, number, position, age, height, weight, and club selection.
<code><span><form class="form-vertical" action="?id={{qy.id}}" method="post"></span>
<span> <span><div class="form-group col-md-6 col-md-offset-3"></span>
<span> <span><lable>球员姓名</lable></span>
<span> <span><input type="text" class="form-control" placeholder="球员姓名" name="xingming" value="{{qy.xingming}}"></span>
<span> <span></div></span>
<span> <span><div class="form-group col-md-6 col-md-offset-3"></span>
<span> <span><lable>球员国籍</lable></span>
<span> <span><input type="text" class="form-control" placeholder="球员国籍" name="guoji" value="{{qy.guoji}}"></span>
<span> <span></div></span>
<span> <span><div class="form-group col-md-6 col-md-offset-3"></span>
<span> <span><lable>球衣号码</lable></span>
<span> <span><input type="text" class="form-control" placeholder="球衣号码" name="haoma" value="{{qy.haoma}}"></span>
<span> <span></div></span>
<span> <span><div class="form-group col-md-6 col-md-offset-3"></span>
<span> <span><lable>场上位置</lable></span>
<span> <span><input type="text" class="form-control" placeholder="场上位置" name="weizhi" value="{{qy.weizhi}}"></span>
<span> <span></div></span>
<span> <span><div class="form-group col-md-6 col-md-offset-3"></span>
<span> <span><lable>球员年龄</lable></span>
<span> <span><input type="text" class="form-control" placeholder="球员年龄" name="nianling" value="{{qy.nianling}}"></span>
<span> <span></div></span>
<span> <span><div class="form-group col-md-6 col-md-offset-3"></span>
<span> <span><lable>球员身高</lable></span>
<span> <span><input type="text" class="form-control" placeholder="球员身高" name="shenggao" value="{{qy.shenggao}}"></span>
<span> <span></div></span>
<span> <span><div class="form-group col-md-6 col-md-offset-3"></span>
<span> <span><lable>球员体重</lable></span>
<span> <span><input type="text" class="form-control" placeholder="球员体重" name="tizhong" value="{{qy.tizhong}}"></span>
<span> <span></div></span>
<span> <span><div class="form-group col-md-6 col-md-offset-3"></span>
<span> <span><lable>所在俱乐部</lable></span>
<span> <span><select class="form-control" name="jlb_id"></span>
<span> {% for jj in jianjies %}</span>
<span> {% if jj.id == qy.jlb_id %}</span>
<span> <option value="{{jj.id}}" selected="selected">{{jj.mingcheng}}</option></span>
<span> {% else %}</span>
<span> <option value="{{jj.id}}">{{jj.mingcheng}}</option></span>
<span> {% endif %}</span>
<span> {% endfor %}</span>
<span> </select></span>
<span> </div></span>
<span> <span><div class="form-group col-md-6 col-md-offset-3"></span>
<span> <span><input type="submit" class="form-control btn btn-danger" value="修改"></span>
<span> </div></span>
<span></form></span></code>The article also includes screenshots of the admin interface, front‑end page, and login screen to illustrate the system’s appearance.
Finally, a promotional note encourages readers to follow the public account, star the post, and scan a QR code to receive free Python learning resources.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.