Simple Basketball Information Management System with CRUD and Pagination Using Django
This article presents a complete Django‑based basketball information management system that implements player data CRUD operations, pagination, and front‑end templates, providing full code examples for view functions, HTML tables, and edit forms to help developers build similar web applications.
The article introduces a simple basketball information management system built with Django, aiming to share the implementation details and invite feedback from fellow developers.
Features : the system supports creating, reading, updating, and deleting player records, and displays the data on a front‑end page with pagination.
Main code (view function) :
def qiuyuan(request):
qiuyuans = models.qiuyuan.objects.all()
pn = request.GET.get('pn', 1)
try:
pn = int(pn)
except:
pn = 1
# search
keyword = request.GET.get('keyword', None)
if keyword is not None:
qiuyuans = models.qiuyuan.objects.filter(xingming__icontains=keyword).all()
else:
qiuyuans = models.qiuyuan.objects.all()
# pagination
paginator = Paginator(qiuyuans, 5) # a1: query set, a2: items per page
try:
qiuyuans = paginator.page(pn) # get records of the requested page
except (EmptyPage, InvalidPage, PageNotAnInteger) as e:
pn = 1
qiuyuans = paginator.page(pn)
num_pages = qiuyuans.paginator.num_pages
# pagination number display logic
if num_pages >= 5:
if pn <= 2:
start = 1
end = 6
elif pn > num_pages - 2:
start = num_pages - 1
end = num_pages + 1
else:
start = pn - 2
end = pn + 3
else:
start = 1
end = num_pages + 1
numbers = range(start, end)
context = {
'qiuyuan': 'active',
'qiuyuans': qiuyuans,
'num_pages': num_pages,
'numbers': numbers,
'pn': pn,
}
return render(request, 'houtai/qiuyuan.html', context)2. Add Information Template (HTML table with Django template tags):
{% extends 'houtai/base.html' %}
{% block main %}
<table class="table table-bordered" method="post">
<tr>
<th>ID</th><th>球员姓名</th><th>球员国籍</th><th>球衣号码</th><th>场上位置</th><th>球员年龄</th><th>球员身高</th><th>球员体重</th><th>所在俱乐部</th><th>操作</th>
</tr>
{% for qy in qiuyuans %}
<tr>
<td>{{ qy.id }}</td>
<td>{{ qy.xingming }}</td>
<td>{{ qy.guoji }}</td>
<td>{{ qy.haoma }}</td>
<td>{{ qy.weizhi }}</td>
<td>{{ qy.nianling }}</td>
<td>{{ qy.shenggao }}</td>
<td>{{ qy.tizhong }}</td>
<td>{{ qy.jlb.mingcheng }}</td>
<td><a href="{% url 'houtai:qy_del' %}?id={{qy.id}}" onclick="return confirm('你确定要删除吗')">删除</a> <a href="{% url 'qy_edit' %}?id={{qy.id}}">修改</a></td>
</tr>
{% endfor %}
</table>
<tr><submit><a href="{% url 'qy_add' %}">添加</a></submit></tr>
{% include 'houtai/qy_page.html' %}
{% endblock %}3. Edit Information Form (HTML form with Django variables):
<form class="form-vertical" action="?id={{qy.id}}" method="post">
<div class="form-group col-md-6 col-md-offset-3">
<lable>球员姓名</lable>
<input type="text" class="form-control" placeholder="球员姓名" name="xingming" value="{{qy.xingming}}">
</div>
<div class="form-group col-md-6 col-md-offset-3">
<lable>球员国籍</lable>
<input type="text" class="form-control" placeholder="球员国籍" name="guoji" value="{{qy.guoji}}">
</div>
<!-- additional fields for number, position, age, height, weight -->
<div class="form-group col-md-6 col-md-offset-3">
<lable>所在俱乐部</lable>
<select class="form-control" name="jlb_id">
{% for jj in jianjies %}
{% if jj.id == qy.jlb_id %}
<option value="{{jj.id}}" selected="selected">{{jj.mingcheng}}</option>
{% else %}
<option value="{{jj.id}}">{{jj.mingcheng}}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="form-group col-md-6 col-md-offset-3">
<input type="submit" class="form-control btn btn-danger" value="修改">
</div>
</form>The article also includes screenshots of the administrator interface, front‑end pages, and login page, demonstrating the visual result of the system.
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.