diff --git a/src/wq/mysite/db.sqlite3 b/src/wq/mysite/db.sqlite3 new file mode 100644 index 00000000..feb0e00a Binary files /dev/null and b/src/wq/mysite/db.sqlite3 differ diff --git a/src/wq/mysite/manage.py b/src/wq/mysite/manage.py new file mode 100644 index 00000000..a7da6671 --- /dev/null +++ b/src/wq/mysite/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/src/wq/mysite/myapp/Login.py b/src/wq/mysite/myapp/Login.py new file mode 100644 index 00000000..9b7178b6 --- /dev/null +++ b/src/wq/mysite/myapp/Login.py @@ -0,0 +1,27 @@ +# import json # 确保导入了json模块 +# from django.shortcuts import render +# from django.http import JsonResponse +# from django.views.decorators.csrf import csrf_exempt +# from django.http import HttpResponseBadRequest + +# @csrf_exempt # 如果需要跨域且没有CSRF中间件 +# def login(request): +# if request.method == 'POST': +# try: +# # 解析 JSON 数据 +# data = json.loads(request.body) +# account = data.get('account') +# password = data.get('password') +# #因后端数据库还没有实现,先只进行简单的验证 +# if account == '18879439530' and password == 'xiaofan123.': +# return JsonResponse({'message': '登录成功'}) +# else: +# return JsonResponse({'message': '登录失败', 'reason': '账号或密码不正确'}) # 使用JsonResponse并设置状态码 +# except KeyError as e: +# # 如果有任何键不存在,返回400错误 +# return JsonResponse({'message': f'缺少必要的参数: {e}'}, status=400) +# except json.JSONDecodeError: # 添加异常处理来捕获JSON解析错误 +# return JsonResponse({'message': '无效的JSON数据'}, status=400) + +# # 处理非POST请求的情况 +# return JsonResponse({'message': '不支持的请求方法'}, status=405) # 对于非POST请求,返回405状态码 \ No newline at end of file diff --git a/src/wq/mysite/myapp/__init__.py b/src/wq/mysite/myapp/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/wq/mysite/myapp/__pycache__/Login.cpython-311.pyc b/src/wq/mysite/myapp/__pycache__/Login.cpython-311.pyc new file mode 100644 index 00000000..760d9e68 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/Login.cpython-311.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/Login.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/Login.cpython-312.pyc new file mode 100644 index 00000000..35f1cae2 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/Login.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/Register.cpython-311.pyc b/src/wq/mysite/myapp/__pycache__/Register.cpython-311.pyc new file mode 100644 index 00000000..beee8ab2 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/Register.cpython-311.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/Register.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/Register.cpython-312.pyc new file mode 100644 index 00000000..69e8fa52 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/Register.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/__init__.cpython-311.pyc b/src/wq/mysite/myapp/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 00000000..aac4253a Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/__init__.cpython-311.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/__init__.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 00000000..064dfb80 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/__init__.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/admin.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/admin.cpython-312.pyc new file mode 100644 index 00000000..d121ba35 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/admin.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/apps.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/apps.cpython-312.pyc new file mode 100644 index 00000000..86578038 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/apps.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/backends.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/backends.cpython-312.pyc new file mode 100644 index 00000000..1885e324 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/backends.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/middlewares.cpython-311.pyc b/src/wq/mysite/myapp/__pycache__/middlewares.cpython-311.pyc new file mode 100644 index 00000000..9d35ddca Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/middlewares.cpython-311.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/middlewares.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/middlewares.cpython-312.pyc new file mode 100644 index 00000000..20d01d27 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/middlewares.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/models.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/models.cpython-312.pyc new file mode 100644 index 00000000..5e19620d Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/models.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/urls.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/urls.cpython-312.pyc new file mode 100644 index 00000000..2a54c492 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/urls.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/views.cpython-311.pyc b/src/wq/mysite/myapp/__pycache__/views.cpython-311.pyc new file mode 100644 index 00000000..163216b8 Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/views.cpython-311.pyc differ diff --git a/src/wq/mysite/myapp/__pycache__/views.cpython-312.pyc b/src/wq/mysite/myapp/__pycache__/views.cpython-312.pyc new file mode 100644 index 00000000..af04c51e Binary files /dev/null and b/src/wq/mysite/myapp/__pycache__/views.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/admin.py b/src/wq/mysite/myapp/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/src/wq/mysite/myapp/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/src/wq/mysite/myapp/apps.py b/src/wq/mysite/myapp/apps.py new file mode 100644 index 00000000..c34fb20e --- /dev/null +++ b/src/wq/mysite/myapp/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MyappConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'myapp' diff --git a/src/wq/mysite/myapp/backends.py b/src/wq/mysite/myapp/backends.py new file mode 100644 index 00000000..0a4b3648 --- /dev/null +++ b/src/wq/mysite/myapp/backends.py @@ -0,0 +1,16 @@ +from django.http import HttpRequest +from myapp.views import login_view + +class MyBackend: + def authenticate(self, account=None, password=None): + # 创建一个虚拟的HttpRequest对象 + request = HttpRequest() + request.method = 'POST' # 设置请求方法为POST,以便符合login_view()函数的期望 + + credentials = {'account': account, 'password': password} + + # 将credentials作为POST数据传递给虚拟的HttpRequest对象 + request.POST = credentials + + user = login_view(request) # 调用login_view()函数来处理身份验证 + return user if user is not None else None \ No newline at end of file diff --git a/src/wq/mysite/myapp/middlewares.py b/src/wq/mysite/myapp/middlewares.py new file mode 100644 index 00000000..d6e175b6 --- /dev/null +++ b/src/wq/mysite/myapp/middlewares.py @@ -0,0 +1,56 @@ +class CorsMiddleware(object): + """中间件:跨域访问""" + def __init__(self, get_response): + self.get_response = get_response + + # 你可以在这里定义默认允许的跨域方法、头部等 + self.allowed_origins = ['*'] # 可以是具体的域名列表,如 ['http://example.com', 'http://localhost:5173'] + self.allowed_methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] + self.allowed_headers = ['Content-Type', 'Authorization', 'X-Requested-With'] + self.allow_credentials = True # 如果允许携带凭据(如cookies),则设置为True + + def __call__(self, request): + response = self.get_response(request) + # 对于CORS预检请求(OPTIONS),可以在这里直接返回响应 + if request.method == 'OPTIONS': + response = self.process_options(request, response) + else: + response = self.process_response(request, response) + return response + + def process_options(self, request, response): + # 处理CORS预检请求 + if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META: + response['Access-Control-Allow-Methods'] = ', '.join(self.allowed_methods) + if 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' in request.META: + allowed_headers = request.META['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'].split(',') + allowed_headers = [x.strip() for x in allowed_headers] + response['Access-Control-Allow-Headers'] = ', '.join( + set(allowed_headers).intersection(self.allowed_headers) + ) + response['Access-Control-Allow-Origin'] = self.get_origin(request) + if self.allow_credentials: + response['Access-Control-Allow-Credentials'] = 'true' + response['Access-Control-Max-Age'] = 86400 # 预检请求的缓存时间,单位:秒 + return response + + def process_response(self, request, response): + # 处理其他类型的响应 + response['Access-Control-Allow-Origin'] = self.get_origin(request) + if self.allow_credentials: + response['Access-Control-Allow-Credentials'] = 'true' + return response + + def get_origin(self, request): + # 获取允许的来源 + origin = request.META.get('HTTP_ORIGIN') + if origin in self.allowed_origins or '*' in self.allowed_origins: + return origin + return '' + +# 在Django的settings.py中,将CorsMiddleware添加到MIDDLEWARE中 +MIDDLEWARE = [ + # ... + 'path.to.CorsMiddleware', # 替换为CorsMiddleware所在的完整路径 + # ... +] \ No newline at end of file diff --git a/src/wq/mysite/myapp/migrations/0001_initial.py b/src/wq/mysite/myapp/migrations/0001_initial.py new file mode 100644 index 00000000..5206e44e --- /dev/null +++ b/src/wq/mysite/myapp/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# Generated by Django 5.0.4 on 2024-05-16 02:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='mod', + fields=[ + ('id', models.AutoField(primary_key=True, serialize=False)), + ('user', models.CharField(max_length=255)), + ('password', models.IntegerField()), + ], + ), + ] diff --git a/src/wq/mysite/myapp/migrations/0002_mytable.py b/src/wq/mysite/myapp/migrations/0002_mytable.py new file mode 100644 index 00000000..4c2de205 --- /dev/null +++ b/src/wq/mysite/myapp/migrations/0002_mytable.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.4 on 2024-05-16 03:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('myapp', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='MyTable', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('field1', models.CharField(max_length=100)), + ('field2', models.IntegerField()), + ], + options={ + 'db_table': 'my_table', + }, + ), + ] diff --git a/src/wq/mysite/myapp/migrations/0003_passenger_delete_mytable.py b/src/wq/mysite/myapp/migrations/0003_passenger_delete_mytable.py new file mode 100644 index 00000000..eecaa14e --- /dev/null +++ b/src/wq/mysite/myapp/migrations/0003_passenger_delete_mytable.py @@ -0,0 +1,28 @@ +# Generated by Django 5.0.4 on 2024-05-16 16:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('myapp', '0002_mytable'), + ] + + operations = [ + migrations.CreateModel( + name='Passenger', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('account', models.CharField(max_length=100)), + ('password', models.CharField(max_length=100)), + ('mobileno', models.CharField(max_length=20)), + ('bankcardno', models.CharField(max_length=100)), + ('idcardno', models.CharField(max_length=100)), + ], + ), + migrations.DeleteModel( + name='MyTable', + ), + ] diff --git a/src/wq/mysite/myapp/migrations/0004_alter_passenger_account.py b/src/wq/mysite/myapp/migrations/0004_alter_passenger_account.py new file mode 100644 index 00000000..6ca055f0 --- /dev/null +++ b/src/wq/mysite/myapp/migrations/0004_alter_passenger_account.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.4 on 2024-05-16 18:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('myapp', '0003_passenger_delete_mytable'), + ] + + operations = [ + migrations.AlterField( + model_name='passenger', + name='account', + field=models.CharField(max_length=100, null=True), + ), + ] diff --git a/src/wq/mysite/myapp/migrations/0005_alter_passenger_bankcardno_alter_passenger_idcardno_and_more.py b/src/wq/mysite/myapp/migrations/0005_alter_passenger_bankcardno_alter_passenger_idcardno_and_more.py new file mode 100644 index 00000000..dd33fab7 --- /dev/null +++ b/src/wq/mysite/myapp/migrations/0005_alter_passenger_bankcardno_alter_passenger_idcardno_and_more.py @@ -0,0 +1,38 @@ +# Generated by Django 5.0.4 on 2024-05-16 18:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('myapp', '0004_alter_passenger_account'), + ] + + operations = [ + migrations.AlterField( + model_name='passenger', + name='bankcardno', + field=models.CharField(max_length=100, null=True), + ), + migrations.AlterField( + model_name='passenger', + name='idcardno', + field=models.CharField(max_length=100, null=True), + ), + migrations.AlterField( + model_name='passenger', + name='mobileno', + field=models.CharField(max_length=20, null=True), + ), + migrations.AlterField( + model_name='passenger', + name='name', + field=models.CharField(max_length=100, null=True), + ), + migrations.AlterField( + model_name='passenger', + name='password', + field=models.CharField(max_length=100, null=True), + ), + ] diff --git a/src/wq/mysite/myapp/migrations/__init__.py b/src/wq/mysite/myapp/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/wq/mysite/myapp/migrations/__pycache__/0001_initial.cpython-312.pyc b/src/wq/mysite/myapp/migrations/__pycache__/0001_initial.cpython-312.pyc new file mode 100644 index 00000000..cf9ab7c0 Binary files /dev/null and b/src/wq/mysite/myapp/migrations/__pycache__/0001_initial.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/migrations/__pycache__/0002_mytable.cpython-312.pyc b/src/wq/mysite/myapp/migrations/__pycache__/0002_mytable.cpython-312.pyc new file mode 100644 index 00000000..2a594277 Binary files /dev/null and b/src/wq/mysite/myapp/migrations/__pycache__/0002_mytable.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/migrations/__pycache__/0003_passenger_delete_mytable.cpython-312.pyc b/src/wq/mysite/myapp/migrations/__pycache__/0003_passenger_delete_mytable.cpython-312.pyc new file mode 100644 index 00000000..860425d5 Binary files /dev/null and b/src/wq/mysite/myapp/migrations/__pycache__/0003_passenger_delete_mytable.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/migrations/__pycache__/0004_alter_passenger_account.cpython-312.pyc b/src/wq/mysite/myapp/migrations/__pycache__/0004_alter_passenger_account.cpython-312.pyc new file mode 100644 index 00000000..5f4c1f0c Binary files /dev/null and b/src/wq/mysite/myapp/migrations/__pycache__/0004_alter_passenger_account.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/migrations/__pycache__/0005_alter_passenger_bankcardno_alter_passenger_idcardno_and_more.cpython-312.pyc b/src/wq/mysite/myapp/migrations/__pycache__/0005_alter_passenger_bankcardno_alter_passenger_idcardno_and_more.cpython-312.pyc new file mode 100644 index 00000000..0988b221 Binary files /dev/null and b/src/wq/mysite/myapp/migrations/__pycache__/0005_alter_passenger_bankcardno_alter_passenger_idcardno_and_more.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/migrations/__pycache__/__init__.cpython-312.pyc b/src/wq/mysite/myapp/migrations/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 00000000..d988bf31 Binary files /dev/null and b/src/wq/mysite/myapp/migrations/__pycache__/__init__.cpython-312.pyc differ diff --git a/src/wq/mysite/myapp/models.py b/src/wq/mysite/myapp/models.py new file mode 100644 index 00000000..20f1ae31 --- /dev/null +++ b/src/wq/mysite/myapp/models.py @@ -0,0 +1,31 @@ +from django.db import models + +class mod(models.Model): + id = models.AutoField(primary_key=True) + user= models.CharField(max_length=255) + password= models.IntegerField() + + # 数据输出样式 + def __str__(self): + return f"['id': {self.id}, 'user': {self.user}, 'password': {self.password}]" + + class Meta: + db_table = 'us_pass' + # 指定数据表名,可以是需要新建表的表名 + # 也可以是数据库中已存在的表 + # 但变量名需要和数据库中保持一致。 + + +from django.db import models + +class Passenger(models.Model): + name = models.CharField(max_length=100, null=True) + account = models.CharField(max_length=100, null=True) + password = models.CharField(max_length=100, null=True) + mobileno = models.CharField(max_length=20, null=True) + bankcardno = models.CharField(max_length=100, null=True) + idcardno = models.CharField(max_length=100, null=True) + # 添加更多字段... + + # def __str__(self): + # return self.name \ No newline at end of file diff --git a/src/wq/mysite/myapp/temphtml/home.html b/src/wq/mysite/myapp/temphtml/home.html new file mode 100644 index 00000000..6c55d8c1 --- /dev/null +++ b/src/wq/mysite/myapp/temphtml/home.html @@ -0,0 +1,7 @@ +
+ + {% csrf_token %} + name:
+ password:
+ +
\ No newline at end of file diff --git a/src/wq/mysite/myapp/testdb.py b/src/wq/mysite/myapp/testdb.py new file mode 100644 index 00000000..23d08f4f --- /dev/null +++ b/src/wq/mysite/myapp/testdb.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +from django.http import HttpResponse + +from TestModel.models import Test + +# 数据库操作 +def testdb(request): + test1 = Test(name='runoob') + test1.save() + return HttpResponse("

数据添加成功!

") \ No newline at end of file diff --git a/src/wq/mysite/myapp/tests.py b/src/wq/mysite/myapp/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/src/wq/mysite/myapp/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/src/wq/mysite/myapp/urls.py b/src/wq/mysite/myapp/urls.py new file mode 100644 index 00000000..42c186f6 --- /dev/null +++ b/src/wq/mysite/myapp/urls.py @@ -0,0 +1,9 @@ +# passengers/urls.py +from django.urls import path +from . import views +# from myapp.views import save_passenger + +urlpatterns = [ + path('save-passenger/', views.save_passenger, name='save_passenger'), + path('login/', views.login_view, name='login'), +] \ No newline at end of file diff --git a/src/wq/mysite/myapp/views.py b/src/wq/mysite/myapp/views.py new file mode 100644 index 00000000..202c1a83 --- /dev/null +++ b/src/wq/mysite/myapp/views.py @@ -0,0 +1,75 @@ +# myapp/views.py +from django.views.decorators.csrf import csrf_exempt +from django.http import HttpResponseBadRequest +from django.http import HttpResponse +from django.contrib.auth import authenticate, login +# from django.contrib.auth.models import User +from django.shortcuts import render +from .models import Passenger +import json # 确保导入了json模块 +from django.http import JsonResponse +from myapp.models import Passenger +@csrf_exempt # 如果需要跨域且没有CSRF中间件 +def save_passenger(request): + if request.method == 'POST': + try: + # 解析 JSON 数据 + data = json.loads(request.body) + name = data.get('name') + account = data.get('PhoneNO') + idcardno = data.get('idCard') + mobileno = data.get('PhoneNO') + password = data.get('password') + bankcardno = data.get('Bankcard') + + # 创建Passenger对象并保存到数据库 + passenger = Passenger( + name=name, + account=account, + password=password, + mobileno=mobileno, + bankcardno=bankcardno, + idcardno=idcardno + ) + passenger.save() + + return JsonResponse({'message': '注册成功'}) + # else: + # return JsonResponse({'message': '注册失败', 'reason': '姓名或身份证号不正确'}) # 使用JsonResponse并设置状态码 + except KeyError as e: + # 如果有任何键不存在,返回400错误 + return JsonResponse({'message': f'缺少必要的参数: {e}'}, status=400) + except json.JSONDecodeError: # 添加异常处理来捕获JSON解析错误 + return JsonResponse({'message': '无效的JSON数据'}, status=400) + + # 处理非POST请求的情况 + return JsonResponse({'message': '不支持的请求方法'}, status=405) # 对于非POST请求,返回405状态码 + + +def login_view(request): + if request.method == 'POST': + try: + # 解析 JSON 数据 + data = json.loads(request.body) + account = data.get('account') + password = data.get('password') + # 查询 passenger 表,查找匹配的账号和密码 + # passenger = Passenger.objects.filter(account=account, password=password).first() + passenger = Passenger.objects.get(account=account, password=password) + if passenger is not None: + # user = authenticate(request, account=passenger.account, password=passenger.password) + # if user is not None: + # login(request, user) + return JsonResponse({'message': '登录成功'}) + else : + return JsonResponse({'message': '登录失败', 'reason': '账号或密码不正确'}) # 使用JsonResponse并设置状态码 + # else: + # return JsonResponse({'message': '登录失败', 'reason': '账号或密码不正确'}) # 使用JsonResponse并设置状态码 + except KeyError as e: + # 如果有任何键不存在,返回400错误 + return JsonResponse({'message': f'缺少必要的参数: {e}'}, status=400) + except json.JSONDecodeError: # 添加异常处理来捕获JSON解析错误 + return JsonResponse({'message': '无效的JSON数据'}, status=400) + + # 处理非POST请求的情况 + return JsonResponse({'message': '不支持的请求方法'}, status=405) diff --git a/src/wq/mysite/mysite/__init__.py b/src/wq/mysite/mysite/__init__.py new file mode 100644 index 00000000..c45523b2 --- /dev/null +++ b/src/wq/mysite/mysite/__init__.py @@ -0,0 +1,2 @@ +import pymysql +pymysql.install_as_MySQLdb() \ No newline at end of file diff --git a/src/wq/mysite/mysite/__pycache__/__init__.cpython-311.pyc b/src/wq/mysite/mysite/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 00000000..a09b5cf9 Binary files /dev/null and b/src/wq/mysite/mysite/__pycache__/__init__.cpython-311.pyc differ diff --git a/src/wq/mysite/mysite/__pycache__/__init__.cpython-312.pyc b/src/wq/mysite/mysite/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 00000000..b80760e4 Binary files /dev/null and b/src/wq/mysite/mysite/__pycache__/__init__.cpython-312.pyc differ diff --git a/src/wq/mysite/mysite/__pycache__/settings.cpython-311.pyc b/src/wq/mysite/mysite/__pycache__/settings.cpython-311.pyc new file mode 100644 index 00000000..4cd5b419 Binary files /dev/null and b/src/wq/mysite/mysite/__pycache__/settings.cpython-311.pyc differ diff --git a/src/wq/mysite/mysite/__pycache__/settings.cpython-312.pyc b/src/wq/mysite/mysite/__pycache__/settings.cpython-312.pyc new file mode 100644 index 00000000..69fc0cdd Binary files /dev/null and b/src/wq/mysite/mysite/__pycache__/settings.cpython-312.pyc differ diff --git a/src/wq/mysite/mysite/__pycache__/urls.cpython-311.pyc b/src/wq/mysite/mysite/__pycache__/urls.cpython-311.pyc new file mode 100644 index 00000000..96efdd5c Binary files /dev/null and b/src/wq/mysite/mysite/__pycache__/urls.cpython-311.pyc differ diff --git a/src/wq/mysite/mysite/__pycache__/urls.cpython-312.pyc b/src/wq/mysite/mysite/__pycache__/urls.cpython-312.pyc new file mode 100644 index 00000000..16eb1b7d Binary files /dev/null and b/src/wq/mysite/mysite/__pycache__/urls.cpython-312.pyc differ diff --git a/src/wq/mysite/mysite/__pycache__/wsgi.cpython-311.pyc b/src/wq/mysite/mysite/__pycache__/wsgi.cpython-311.pyc new file mode 100644 index 00000000..97ecce4b Binary files /dev/null and b/src/wq/mysite/mysite/__pycache__/wsgi.cpython-311.pyc differ diff --git a/src/wq/mysite/mysite/__pycache__/wsgi.cpython-312.pyc b/src/wq/mysite/mysite/__pycache__/wsgi.cpython-312.pyc new file mode 100644 index 00000000..d56a1579 Binary files /dev/null and b/src/wq/mysite/mysite/__pycache__/wsgi.cpython-312.pyc differ diff --git a/src/wq/mysite/mysite/asgi.py b/src/wq/mysite/mysite/asgi.py new file mode 100644 index 00000000..cb29d225 --- /dev/null +++ b/src/wq/mysite/mysite/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for mysite project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_asgi_application() diff --git a/src/wq/mysite/mysite/settings.py b/src/wq/mysite/mysite/settings.py new file mode 100644 index 00000000..2efb5389 --- /dev/null +++ b/src/wq/mysite/mysite/settings.py @@ -0,0 +1,170 @@ +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 5.0.6. + +For more information on this file, see +https://docs.djangoproject.com/en/5.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-wub5jg!ye3iu5=e$a7c0!mx^)x*3r(er%1s_nph@h+rp*_lvql' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['localhost', '127.0.0.1', '192.168.251.7'] +# ALLOWED_HOSTS = ['*'] + +# Application definition + + +# DATABASES = { +# 'default': { +# 'ENGINE': 'django.db.backends.mysql', # 使用MySQL作为数据库引擎 +# 'NAME': 'mini12306', # 数据库名 +# 'USER': 'root', # 数据库用户名 +# 'PASSWORD': 'luoyi091821', # 数据库密码 +# # 'HOST': '192.168.43.196', # 数据库主机名或IP地址 +# 'HOST': '127.0.0.1', +# 'PORT': '3306', # 数据库端口号(MySQL默认是3306) +# 'OPTIONS': { +# 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", # 初始化命令,根据需要修改 +# 'charset': 'utf8mb4', # 设置字符集,推荐使用utf8mb4来支持emoji等 +# }, +# } +# } + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', # 使用MySQL作为数据库引擎 + 'NAME': 'mini12306', # 这就是你要访问的数据库名   + 'USER': 'root', # 替换为你的数据库用户名   + 'PASSWORD': 'luoyi091821', # 替换为你的数据库密码   + 'HOST': 'localhost', # 替换为远程数据库服务器的实际IP地址或域名   + 'PORT': '3306', # 如果MySQL服务器不是使用默认端口,请更改此值   + 'OPTIONS': { + 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", # 初始化命令,根据需要修改 + 'charset': 'utf8mb4', # 设置字符集,推荐使用utf8mb4来支持emoji等 + }, + } +} + +CORS_ORIGIN_WHITELIST = ( + 'http://localhost:5173', + # 可以添加其他源 +) + +INSTALLED_APPS = [ + 'corsheaders', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'myapp', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'corsheaders.middleware.CorsMiddleware', + 'django.middleware.common.CommonMiddleware', + # 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'myapp.middlewares.CorsMiddleware', # 允许跨域访问中间件 +] + +ROOT_URLCONF = 'mysite.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'mysite.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/5.0/ref/settings/#databases + +# DATABASES = { +# 'default': { +# 'ENGINE': 'django.db.backends.sqlite3', +# 'NAME': BASE_DIR / 'db.sqlite3', +# } +# } + + +# Password validation +# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/5.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/5.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + + +AUTHENTICATION_BACKENDS = [ + 'django.contrib.auth.backends.ModelBackend', # 将默认的身份验证后端添加到列表中 + 'myapp.backends.MyBackend', # 添加你自己定义的身份验证后端路径 +] \ No newline at end of file diff --git a/src/wq/mysite/mysite/urls.py b/src/wq/mysite/mysite/urls.py new file mode 100644 index 00000000..23139a63 --- /dev/null +++ b/src/wq/mysite/mysite/urls.py @@ -0,0 +1,38 @@ +""" +URL configuration for mysite project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path,include +from django.contrib import admin +from myapp import Login +from myapp import views +from myapp.views import save_passenger +from myapp.views import login_view +# from myapp.views import register_view +app_name = 'myapp' + +urlpatterns = [ + path('admin/', admin.site.urls), + # path('validate_idcard/', Register.validate_idcard, name='validate_idcard'), + # path('login/', Login.login, name='login'), + path('save_passenger/', views.save_passenger, name='save_passenger'), + path('login_view/', views.login_view, name='login_view'), + # path('register_view/', views.register_view, name='register_view'), + # path('', include('myapp.urls')), + path('myapp/', include('myapp.urls')), +] + + diff --git a/src/wq/mysite/mysite/wsgi.py b/src/wq/mysite/mysite/wsgi.py new file mode 100644 index 00000000..19250374 --- /dev/null +++ b/src/wq/mysite/mysite/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for mysite project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_wsgi_application()