django鍓嶅悗绔垎绂?form_03(楠岃瘉锛屾暟鎹煡璇紝浠g爜浼樺寲)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django鍓嶅悗绔垎绂?form_03(楠岃瘉锛屾暟鎹煡璇紝浠g爜浼樺寲)相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/elf' title='elf'>elf   鍒嗙   绮剧‘   errors   short   楠岃瘉   param   har   绫诲瀷   

1.浼樺寲浠g爜

鎶奻orm楠岃瘉鐨勮繑鍥炴姤閿欏啓鎴愪竴涓叡鐢ㄧ殑绫?璇ョ被鍦ㄥ伐绋嬩笅寤虹珛浜嗕竴涓猽itls-tools.py

class FormatErrMsg:

    @property #瑁呴グ鍣?灞炴€ф柟娉?璋冪敤鐨勬椂鍊欎笉闇€瑕佸姞锛堬級
    def error_msg(self):
        #self.get_json_data() 鏄痜orm鑷甫鐨勫弸濂界殑鎶ラ敊鎻愮ず 杩斿洖鐨勬槸涓€涓瓧鍏?/span>
        message = 鈥樷€?/span>
        for error_params, v in self.errors.get_json_data().items():
            error_message = v[0].get(鈥?/span>message鈥?/span>)
            m = 鈥?/span>%s:%s鈥?/span> % (error_params, error_message)
            message += m
        return message

2.鍗曠嫭鍦ㄥ伐绋嬩笅闈㈠缓绔嬩竴涓猣orms.py鏂囦欢锛岀敤鏉ヤ笓闂ㄥ瓨鏀緁orm楠岃瘉锛岃繖鏍峰氨淇濊瘉views閲屽彧鍐欓€昏緫锛岃€屼笉娑夊強楠岃瘉

#鍒涘缓杩欎釜鏂囦欢涓撻棬鍐檉orms 楠岃瘉鏁版嵁
from django import forms

from uitls.tools import FormatErrMsg
from . import models

#鐢ㄥ叧鑱旀暟鎹簱鐨勭害鏉熼獙璇乫orm
class CaseSetFormALL(forms.ModelForm,FormatErrMsg):
    class Meta:
        fields = 鈥?/span>__all__鈥?/span>#鍏宠仈浜嗘墍鏈夊瓧娈?/span>
        model = models.CaseSet#鍏宠仈鏁版嵁搴揅aseSet
        exclude = [鈥?/span>is_delete鈥?/span>,鈥?/span>create_time鈥?/span>,鈥?/span>update_time鈥?/span>]#鎺掓煡涓嶆牎楠岀殑瀛楁

class CaseFormALL(forms.ModelForm,FormatErrMsg):
    class Meta:
        fields = 鈥?/span>__all__鈥?/span>
        model = models.Case
        exclude = [鈥?/span>case_set鈥?/span>,鈥?/span>create_time鈥?/span>,鈥?/span>update_time鈥?/span>]

#鑷畾涔?/span>
class CaseForm(forms.Form,FormatErrMsg):
    鈥樷€樷€?/span>鏍¢獙璇锋眰鍙傛暟鈥樷€樷€?/span>
    title = forms.CharField(max_length=50,min_length=2)
    desc = forms.CharField(max_length=70,required=False)#required=False 榛樿desc鍙互涓虹┖
    method = forms.IntegerField()
    url = forms.URLField()
    params = forms.CharField(max_length=100)

    #鑷畾涔夋牎楠?/span>
    #鍗曚釜鍙傛暟鏍¢獙@1
    # def clean_method(self):
    #     method = self.cleaned_data.get(鈥榤ethod鈥?#鍏堟牎楠屼笂杩伴偅浜涘熀鏈殑鏍煎紡锛岀劧鍚庡湪鏍¢獙鑷畾涔夌殑瑙勫垯
    #     if method not in (0,1,2,3):
    #         raise ValidationError(鈥榤ethod鍊间笉瀵光€?
    #     return method
    # 鍗曚釜鍙傛暟鏍¢獙@2
    # def clean_title(self):
    #     title = self.cleaned_data.get(鈥榯itle鈥?
    #     if models.Case.objects.filter(title=title).count()>0:
    #         raise ValidationError(鈥樻爣棰橀噸澶嶁€?
    #     return title
    # def clean(self):#澶氫釜鍙傛暟缁勫悎鏍¢獙鈥榤ethod鈥橈紝鈥榯itle鈥?/span>
    #     method = self.cleaned_data.get(鈥榤ethod鈥?
    #     title = self.cleaned_data.get(鈥榯itle鈥?
    #     if method not in (0,1,2,3):
    #         raise forms.ValidationError(鈥榤ethod鍙傛暟閿欒鈥?#鎶涘嚭寮傚父
    #     elif models.Case.objects.filter(title=title).count()>0:
    #         raise forms.ValidationError(鈥樻爣棰橀噸澶嶁€?#鎶涘嚭寮傚父
    #     return self.cleaned_data

class CaseSetForm(forms.Form,FormatErrMsg):
    name = forms.CharField(max_length=50,min_length=2)
    desc = forms.CharField(max_length=50,required=False)

    def clean_name(self):
        name = self.cleaned_data.get(鈥?/span>name鈥?/span>)
        if models.CaseSet.objects.filter(name=name).count()>0:
            raise forms.ValidationError(鈥?/span>鐢ㄤ緥闆嗗悎鍚嶇О閲嶅鈥?/span>)
        return name

3.鍦╲iews.py涓啓 澧炲垹鏀规煡鐨勯€昏緫

from django.forms import model_to_dict,ValidationError
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from django import forms
from . import forms
from django.core.paginator import Paginator
from . import models
from django.db.models import Q
from uitls.tools import Page
# Create your views here.

#甯歌鍐欐硶@1锛孈2锛岀敤渚嬮泦鍚堣〃
#鐢ㄤ緥闆嗗悎娣诲姞 post璇锋眰 闇€瑕佷紶涓や釜鍙傛暟 鍗抽泦鍚堣〃鐨勫鍔犳搷浣淍1
def case_set(request):
    name = request.POST.get(鈥?/span>name鈥?/span>)
    desc = request.POST.get(鈥?/span>desc鈥?/span>)
    if name.strip() and desc.strip() and models.CaseSet.objects.filter(name=name).count()<1:
        models.CaseSet.objects.create(name=name,desc=desc)
        data = {鈥?/span>code鈥?/span>:0,鈥?/span>msg鈥?/span>:鈥?/span>娣诲姞鎴愬姛鈥?/span>}
    else:
        data = {鈥?/span>code鈥?/span>:-1,鈥?/span>msg鈥?/span>:鈥?/span>娣诲姞澶辫触鈥?/span>}
    return JsonResponse(data)

#鏌ヨ闆嗗悎 get璇锋眰 涓嶉渶瑕佷紶鍙?鑾峰彇闆嗗悎琛ㄧ殑鏁版嵁 鏌ヨ鎿嶄綔@2
def case_set_all(request):
    case_sets = models.CaseSet.objects.filter(is_delete=False)#鏌ヨ鎵€鏈夐泦鍚?/span>
    data = []
    #鎶婃煡璇㈠嚭鏉ョ殑杞崲鎴愬瓧鍏搁€氳繃model_to_dict鏂规硶锛屽苟娣诲姞杩沝ata鍒楄〃涓紝鏈€缁堣繑鍥瀃{鈥榵x鈥?xx},{鈥榵x鈥?xx}]
    for c in case_sets:
        d = model_to_dict(c)
        data.append(d)
    response = {鈥?/span>code鈥?/span>:0,鈥?/span>msg鈥?/span>:鈥?/span>鎴愬姛鈥?/span>,鈥?/span>data鈥?/span>:data}
    return JsonResponse(response,json_dumps_params={鈥?/span>ensure_ascii鈥?/span>:False})


#涓€涓嚱鏁板啓涓や釜URL璇锋眰鐨勯€昏緫涓婅堪涓ょ鏂瑰紡鐨勭粨鍚?/span>
def case_set_new(request):
    name = request.POST.get(鈥?/span>name鈥?/span>)
    desc = request.POST.get(鈥?/span>desc鈥?/span>)
    if request.method==鈥?/span>GET鈥?/span>:
        case_sets = models.CaseSet.objects.filter(is_delete=False)  # 鏌ヨ鎵€鏈夐泦鍚?/span>
        data = []
        # 鎶婃煡璇㈠嚭鏉ョ殑杞崲鎴愬瓧鍏搁€氳繃model_to_dict鏂规硶锛屽苟娣诲姞杩沝ata鍒楄〃涓紝鏈€缁堣繑鍥瀃{鈥榵x鈥?xx},{鈥榵x鈥?xx}]
        for c in case_sets:
            d = model_to_dict(c)
            data.append(d)
        response = {鈥?/span>code鈥?/span>: 0, 鈥?/span>msg鈥?/span>: 鈥?/span>鎴愬姛鈥?/span>, 鈥?/span>data鈥?/span>: data}
        return JsonResponse(response, json_dumps_params={鈥?/span>ensure_ascii鈥?/span>: False})
    elif request.method==鈥?/span>POST鈥?/span>:
        if name.strip() and desc.strip() and models.CaseSet.objects.filter(name=name).count()<1:
            models.CaseSet.objects.create(name=name, desc=desc)
            data = {鈥?/span>code鈥?/span>: 0, 鈥?/span>msg鈥?/span>: 鈥?/span>娣诲姞鎴愬姛鈥?/span>}
        else:
            data = {鈥?/span>code鈥?/span>: -1, 鈥?/span>msg鈥?/span>: 鈥?/span>娣诲姞澶辫触鈥?/span>}
        return JsonResponse(data)

#cvb鏂瑰紡锛岀敤渚嬮泦鍚堣〃,鏍规嵁璇锋眰涓嶅悓锛屾墽琛岀浉搴旂殑鎿嶄綔
class CaseSet(View):
    #澧炲姞鏁版嵁
    def post(self,request):
        # form = forms.CaseSetFormALL(request.POST)
        form = forms.CaseSetForm(request.POST)
        if form.is_valid():
            models.CaseSet.objects.create(**form.cleaned_data)
            data = {鈥?/span>code鈥?/span>: 0, 鈥?/span>msg鈥?/span>: 鈥?/span>娣诲姞鎴愬姛鈥?/span>}
        else:
            data = {鈥?/span>code鈥?/span>: -1, 鈥?/span>msg鈥?/span>: form.error_msg}
        return JsonResponse(data)

    def get_paginator(self,data_list):#鍒嗛〉鍑芥暟
        limit = self.request.GET.get(鈥?/span>limit鈥?/span>, 3)  # 榛樿涓嶄紶 姣忛〉鏄剧ず3鏉?/span>
        page_num = self.request.GET.get(鈥?/span>page_num鈥?/span>, 1)  # 榛樿涓嶄紶 鏄剧ず绗竴椤?/span>
        paginator = Paginator(data_list,limit)
        page_data = paginator.page(page_num)
        return page_data,paginator

    def get_search_data(self):#妯$硦鏌ヨ
        data = []
        search = self.request.GET.get(鈥?/span>search鈥?/span>)
        if search:
            data = models.CaseSet.objects.filter(Q(name__contains=search)|Q(desc__contains=search))
        return data

    def get_filter_data(self):#绮剧‘鏌ヨ
        data = []
        # 瀹氫箟绮剧‘鏌ヨ鐨勫瓧娈典负涓€涓垪琛?/span>
        filter_field = [鈥?/span>name鈥?/span>, 鈥?/span>desc鈥?/span>, 鈥?/span>id鈥?/span>]
        filter_dict = {}
        for field in filter_field:
            value = self.request.GET.get(field)
            if value:
                filter_dict[field] = value
        if filter_dict:  # 绮剧‘鏌ヨ
            data = models.CaseSet.objects.filter(**filter_dict)
        return data

    #鏌ユ壘鏁版嵁
    def get(self,request):
        if self.get_filter_data():#璋冪敤绮剧‘鏌ヨ鍑芥暟
            case_sets = self.get_filter_data()
        elif self.get_search_data():#璋冪敤妯$硦鏌ヨ鍑芥暟
            case_sets = self.get_search_data()
        else:
        # 鏌ヨ鎵€鏈?/span>
            case_sets = models.CaseSet.objects.filter(is_delete=False)
        page_data,paginator = self.get_paginator(case_sets)#璋冪敤鍒嗛〉鍑芥暟
        data = []
        # 鎶婃煡璇㈠嚭鏉ョ殑杞崲鎴愬瓧鍏搁€氳繃model_to_dict鏂规硶锛屽苟娣诲姞杩沝ata鍒楄〃涓紝鏈€缁堣繑鍥瀃{鈥榵x鈥?xx},{鈥榵x鈥?xx}]
        for c in page_data:
            d = model_to_dict(c)
            data.append(d)
        response = {鈥?/span>code鈥?/span>: 0, 鈥?/span>msg鈥?/span>: 鈥?/span>鎴愬姛鈥?/span>, 鈥?/span>data鈥?/span>: data,鈥?/span>count鈥?/span>:paginator.count}#count鏄€绘暟鎹?/span>
        return JsonResponse(response, json_dumps_params={鈥?/span>ensure_ascii鈥?/span>: False})

#cvb鏂瑰紡,鐢ㄤ緥琛?/span>
class Case(View):
    #澧炲姞鏁版嵁
    def post(self,request):
        form = forms.CaseForm(request.POST) #閫氳繃post鏂规硶鎷垮埌璇锋眰鐨勬暟鎹?/span>
        # form = forms.CaseFormALL(request.POST)#閫氳繃post鏂规硶鎷垮埌璇锋眰鐨勬暟鎹?/span>
        #寮€濮嬮獙璇?form.is_valid() 杩斿洖涓€涓猙ool绫诲瀷鐨勬暟鎹?閫氳繃杩斿洖True 涓嶉€氳繃杩斿洖False
        if form.is_valid():
            #濡傛灉鏍¢獙閫氳繃浼氳繑鍥炰竴涓瓧鍏竰鈥榯itle鈥?xxx,鈥榙esc鈥?xxx,鈥榤ethod鈥?xx,鈥榰rl鈥?xx,鈥榩arams鈥?xx}
            # print(form.cleaned_data)
            models.Case.objects.create(**form.cleaned_data)#瀛楀吀鍓嶉潰鍔?*杞崲涓簍itle=xxx,desc=xxxx
            data = {鈥?/span>code鈥?/span>: 0, 鈥?/span>msg鈥?/span>: 鈥?/span>鎴愬姛鈥?/span>}
        else:
            data = {鈥?/span>code鈥?/span>: -1, 鈥?/span>msg鈥?/span>: form.error_msg}
            return JsonResponse(data)
    #鏌ユ壘鏁版嵁
    def get(self,request):
        case_sets = models.Case.objects.filter(is_delete=False)  # 鏌ヨ鎵€鏈夐泦鍚?/span>
        data = []
        # 鎶婃煡璇㈠嚭鏉ョ殑杞崲鎴愬瓧鍏搁€氳繃model_to_dict鏂规硶锛屽苟娣诲姞杩沝ata鍒楄〃涓紝鏈€缁堣繑鍥瀃{鈥榵x鈥?xx},{鈥榵x鈥?xx}]
        for c in case_sets:
            d = model_to_dict(c)
            data.append(d)
        response = {鈥?/span>code鈥?/span>: 0, 鈥?/span>msg鈥?/span>: 鈥?/span>鎴愬姛鈥?/span>, 鈥?/span>data鈥?/span>: data}
        return JsonResponse(response, json_dumps_params={鈥?/span>ensure_ascii鈥?/span>: False})
    #鍒犻櫎鏁版嵁
    def delete(self,request):
        id = request.GET.get(鈥?/span>id鈥?/span>)
        models.Case.objects.filter(id=id).update(is_delete=True)
        response = {鈥?/span>code鈥?/span>: 0, 鈥?/span>msg鈥?/span>: 鈥?/span>鎴愬姛鈥?/span>}
        return JsonResponse(response, json_dumps_params={鈥?/span>ensure_ascii鈥?/span>: False})

以上是关于django鍓嶅悗绔垎绂?form_03(楠岃瘉锛屾暟鎹煡璇紝浠g爜浼樺寲)的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 鍓嶅悗绔紑鍙戠郴缁?瑙嗛鏁欑▼

JS 姝e垯楠岃瘉瀛楃涓蹭腑鏄惁鍚湁鏁板瓧

鍏充簬璁粌闆?楠岃瘉闆?娴嬭瘯闆嗙殑鍒掑垎

API 浜や簰涓€庝箞鍋氬ソ鍥剧墖楠岃瘉鐮侊紵