Step-by-Step Guide to Building a Django‑Vue Sports Shop Backend
This tutorial walks through setting up a Django backend for a Vue‑based sports shop, covering environment requirements, project structure, database initialization, API implementation, common pitfalls, and file‑upload handling with complete code snippets and screenshots.
Prerequisite
Environment requirements: MySQL 8, python3.11, django4.2, pymysql
How to Run
1. Download and unzip the project
https://download.csdn.net/download/yanglamei1962/90110708
2. Add the database
Import the sports_shop.sql file located in the project root into your MySQL instance.
3. Modify the database connection
Edit sports_shop_backend_war/dao.py and replace the username and password with your MySQL credentials.
4. Install required libraries
pip install django==4.2 pip install pymysql5. Run the project
The frontend expects the backend API at http://127.0.0.1:8888. From the project root execute: python \manage.py runserver 8888 After the command finishes, the server will be running.
Project Overview
The frontend is built with Vue and its compiled files are placed in the static directory.
The backend uses Django views.py to expose API endpoints; see API接口文档.md for details.
Key modules in the sports_shop_backend_war app:
Pitfalls & Solutions
2023/4/25
Django's ORM generates many auxiliary tables, which differs from typical Spring ORM usage.
2023/4/26
Static files must be served via the static path. Add a redirect in urls.py:
path('', RedirectView.as_view(url='/static/index.html', permanent=False), name='index'),2023/4/27
Import paths in Python require careful handling; use sys.path.append() when accessing sibling packages.
2023/4/28
When importing entities or DAOs, ensure the correct package notation, e.g. from entity.User import User and from dao.UserDao import UserDaoImpl.
2023/4/29
Handling AJAX POST data in Django: request.POST.get('field') may be empty; use request.body and parse JSON, or fallback to request.GET.get('field') for testing.
2023/5/4 – File Upload
Retrieve uploaded files with request.FILES.get('form_name'). The file name is request.FILES.get('form_name').name and the binary data is request.FILES.get('form_name').read().
Save the file without using BASE_DIR from settings (it may be empty). Instead compute the project directory manually:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
upload_path = BASE_DIR + '/static/picture/' + fileNameWrite the file:
with open(upload_path, "wb+") as f:
f.write(fileData)Return a JSON response containing the image URL:
response = {'img': 'http://127.0.0.1:8888/static/picture/' + fileName}
json_str = json.dumps(response, default=lambda o: o.__json__() if isinstance(o, (str,)) else None, ensure_ascii=False)
return HttpResponse(json_str, content_type="application/json")Effect Overview
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
