From d9f1e705ac8cc30450f9f06666d4610a0404e020 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Mon, 31 Jul 2023 18:27:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90ajax=E6=A8=A1=E6=8B=9F?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E7=9A=84=E8=81=94=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- form_demo/urls.py | 2 ++ form_demo/views.py | 14 ++++++++++++ templates/form_demo/ajax_login.html | 35 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 templates/form_demo/ajax_login.html diff --git a/form_demo/urls.py b/form_demo/urls.py index 7da7bde..163717c 100644 --- a/form_demo/urls.py +++ b/form_demo/urls.py @@ -10,4 +10,6 @@ urlpatterns = [ path('userinfomodel/', views.userinfo_model_form), path('userinfomodelsave/', views.userinfo_model_from_save), re_path(r'media/(?P.*)', serve, {'document_root': settings.MEDIA_ROOT}), + path('login/', views.ajax_login), + path('logindata/', views.ajax_login_data) ] diff --git a/form_demo/views.py b/form_demo/views.py index 1a22c20..62bd9ef 100644 --- a/form_demo/views.py +++ b/form_demo/views.py @@ -1,4 +1,5 @@ from django.shortcuts import render +from django.http import JsonResponse from .forms import * @@ -77,3 +78,16 @@ def userinfo_model_from_save(request): print(errors) return render(request, 'form_demo/userinfomodels.html', {'form_obj': f, 'errors': errors}) return render(request, 'form_demo/userinfomodels.html', {'form_obj': f}) + + +def ajax_login(request): + return render(request, 'form_demo/ajax_login.html') + + +def ajax_login_data(request): + username = request.POST.get('username') + password = request.POST.get('password') + if username == 'admin' and password == 'admin123': + return JsonResponse({'code': 1, 'msg': '登录成功'}) + else: + return JsonResponse({'code': 0, 'msg': '登录失败'}) diff --git a/templates/form_demo/ajax_login.html b/templates/form_demo/ajax_login.html new file mode 100644 index 0000000..557b83c --- /dev/null +++ b/templates/form_demo/ajax_login.html @@ -0,0 +1,35 @@ + + + + + Title + +{% csrf_token %} +{% load static%} + + +用户名: +密码: + + + + \ No newline at end of file