Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of form form Construction and CBV pattern in django

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "django form form formation and CBV pattern example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "django form form formation and CBV pattern example analysis" this article.

1. FBV and CBVFBV: the view function writes the logic code as a function CBV: the view writes the logic code as a class, such as CBV: views.pyfrom django.shortcuts import render,HttpResponse,redirectfrom django.views import Viewfrom django.views.decorators.csrf import csrf_exempt,csrf_protectfrom django.utils.decorators import method_decoratorfrom app01 import modelsclass LoginView (View): @ method_decorator (csrf_exempt) def dispatch (self, request, * args, * * kwargs): return super (LoginView Self) .dispatch (request,*args,**kwargs) def get (self,request, * args,**kwargs): return render (request, 'login.html') def post (self,request, * args,**kwargs): user = request.POST.get (' user') pwd = request.POST.get ('pwd') obj = models.UserInfo.objects.filter (username=user) Password=pwd) .first () if obj: request.session ['user_info'] = {' id': obj.id, 'username': obj.username} return redirect (' / users.html') return render (request, 'login.html' {'msg':' screw you'}) such as FBV: views.pydef register (request): if request.method = = "GET": form = RegisterForm () return render (request, 'register.html', {' form': form}) else: response = {'status': True,' data': None 'msg': None} form = RegisterForm (request.POST) if form.is_valid (): print (form.cleaned_data) else: response [' status'] = False response ['msg'] = form.errors return HttpResponse (json.dumps (response)) 2. Form form building uses the import forms module from django.forms import Formfrom django.forms import fieldsfrom django.forms import widgets# to create the form class class UserForm (Form): username = fields.CharField (required=True) Error_messages= {'required':' user name cannot be empty'}, widget = widgets.TextInput (attrs= {'class':' form-control'}) password = fields.CharField (required=True, error_messages= {'required':' mailbox cannot be empty', 'invalid':' mailbox format is incorrect'} Widget= widgets.TextInput (attrs= {'class':' form-control'}) ut_id = fields.ChoiceField (choices= [], widget=widgets.Select (attrs= {'class':' form-control'})) role_id = fields.MultipleChoiceField (choices= [], widget=widgets.SelectMultiple (attrs= {'class':' form-control'}) def _ _ init__ (self, * args) * * kwargs): super (UserForm,self). _ init__ (* args,**kwargs) self.fields ['ut_id']. Choices = models.UserType.objects.values_list (' id','title') self.fields ['role_id']. Choices = models.Role.objects.values_list (' id','caption') Note: username, password, the name value fields.CharField that renders to a page that is an input tag attribute indicates the input text type Fields.ChoiceField means to select the drop-down box fields.MultipleChoiceField means to select multiple drop-down boxes required=True means that the required option error_messages= {'required':' username cannot be empty'} indicates an error prompt widget = widgets.TextInput (attrs= {'class':' form-control'}) indicates the addition of class attributes above is all the contents of the article "sample Analysis of the use of form forms in django and CBV patterns". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report