In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "template inheritance and how to use ModelForm in django". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "template inheritance and ModelForm in django".
Inheritance of templates
When writing html, you will find that many things are the same in many html files, including the introduction of static plug-ins and some simple css styles that do not need to be modified, so that you can introduce relevant templates to facilitate operation.
{% load static%} Title... {% block content%} {% endblock%}
For example, we can introduce it into the template file and write {% block content%} {% endblock%} so that we can directly write the content of div in other similar html.
{% extends "layout.html"%} {% block content%}... {% endblock%} form and ModelForm
In our normal operation, if we want to add data to the relevant list and connect to the database to get it, we have to get it according to the name attribute in the form, for example, I have created so much data.
Class UserInfo (models.Model):''employee form' 'name= models.CharField (verbose_name= "name", max_length=32) age = models.IntegerField (verbose_name= "age") password = models.CharField (verbose_name= "password", max_length=64) account = models.DecimalField (verbose_name= "account balance", max_digits=10,decimal_places=2,default=0) creat_time = models.DateTimeField (verbose_name= "entry time")
Get the data and add it to the database
User = request.POST.get ("user") pwg = request.POST.get ("pwd") account = request.POST.get ("ac") ctime = request.POST.get ("ctime") # get the content is the attribute value of name entered by the user in the form, and you can define models.UserInfo.object.creat (name=user,password=pwd.)
This is too cumbersome and has such problems.
-the data submitted by the user is not verified
-there is no error message on the page
-every field on the page needs to be rewritten.
-the associated data must be manually obtained and displayed on the page
But through the two components that come with django: Form and ModleForm
Usage
In view
From django import formsclass UserAdd (forms.ModelForm): # create a class that inherits forms name = forms.CharField (min_length=3) # here you can create all kinds of forms and write some constraints so that the html page shows the constraints, because I have created the data table in models in my django, so I will not create it here Directly get my datasheet title = forms.CharField (max_length=100, label=' title') content = forms.CharField (widget=forms.Textarea, label=' content') email = forms.EmailField (label=' mailbox') reply = forms.BooleanField (required=False, label=' reply') # get Datasheet class Meta: model = models.UserInfo fields = ["name", "password", "age", "creat_time", "gender" "depart"] def user_add (request):''add users' if request.method = = "GET": form = UserAdd () return render (request, "user_add.html", {'form': form})
The form can be generated automatically by using for loop in html.
{% csrf_token%} {% for field in form%} {{field.label}} {{field}} {% endfor%}
We can also add various styles to the form.
Widgets = {"name": forms.TextInput (attrs= {"class": "form-control"}),} # it may be a bit tedious to write like this and have to be defined multiple times We can use class and object-oriented to write def _ init__ (self,*args,**kwargs): super (). _ init__ (* args,**kwargs) for name,field in self.fields.items (): # or add the judgment condition def _ init__ (self,*args,**kwargs): super (). _ init__ (* args,**kwargs) for name Field in self.fields.items (): if name = = "password": continue field.widget.attrs = {"class": "form-control"}
We can add all kinds of judgments.
Def user_add (request):''add user''if request.method = = "GET": form = UserAdd () return render (request, "user_add.html" {'form': form}) form = UserAdd (data=request.POST) # add to the database if form.is_valid (): form.save () return redirect (' / user/list/') else: return render (request, "user_add.html", {'form': form}) if the condition holds
Here, you can add all the data you write to the database with a form.save ().
Various errors can also be displayed in html with various conditions, such as the span tag here.
{% for field in form%} {{field.label}} {{field}} {{field.errors.0}} {% endfor%}
Check our output. If you meet the conditions, you can join the database. If you do not, an error will be displayed.
We can also change the language of the prompt in the settings
Thank you for your reading, the above is the content of "template inheritance and ModelForm in django". After the study of this article, I believe you have a deeper understanding of how to use template inheritance and ModelForm in django, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.