In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use Django to store the front-end data into the Mysql database, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Create a new template in models.py under app
The specific code is as follows:
Class Apply (models.Model): clas = models.CharField (max_length=32, verbose_name= "class") name= models.CharField (max_length=32, verbose_name= "name") number = models.CharField (max_length=15, verbose_name= "student ID") email = models.CharField (max_length=32, verbose_name= "mailbox") phone = models.CharField (max_length=11, verbose_name= "cell phone number") zhiyuan1 = models.EmailField (max_length=32 Verbose_name= "volunteer one") zhiyuan2 = models.EmailField (max_length=32, verbose_name= "volunteer two")
The clas,name under the Apply class will become the list names in the mysql database through data migration.
two。 Data migration (using Terminal tools included with pycharm) python manage.py makemigrations finalapppython manage.py migrate finalapp
Finalapp is the name of my app. Remember to change it to your own app when you migrate your data.
The following is the mysql database after my data migration. You can see that the finalapp_ application table has been created successfully. Double-click to open to view the table. The newly created table does not have any data. In the picture, my front end is submitted to the database through the form form.
3. Create a new view in views.py under app
The specific code is as follows:
Def apply (request): if request.method = = "POST": # to determine that the request is a POST request is to submit the form apply = Apply () # create an apply instance clas = request.POST.get ("clas") # get the clas value in the submitted form and assign it to clas name = request.POST.get ("name") # get the name value in the submitted form Assign the variable name number = request.POST.get ("number") email = request.POST.get ("email") phone = request.POST.get ("phone") zhiyuan1 = request.POST.get ("zhiyuan1") zhiyuan2 = request.POST.get ("zhiyuan2") apply.clas = clas # assign the instance the value apply.name = name apply.number = number Apply.email = email apply.phone = phone apply.zhiyuan1 = zhiyuan1 apply.zhiyuan2 = zhiyuan2 apply.save () # Save the instance Save the data to the database return render (request, 'applysuccessful.html') # after saving the data, jump from the apply page to the successful page else: return render (request,' apply.html') # when there is no data, jump to the apply page 4. Front end
The specific code is as follows:
{% csrf_token%}
Class :
Last name :
Learn the number:
Mailing box:
Mobile number:
Volunteer one:
Volunteer II:
The form form must write the action attribute, which specifies where to send the form data when the form is submitted.
{% csrf_token%}: in Django, we need to add {% csrf_token%} to the form of templates. Its function is that when we get the form page, the server returns a string of random characters to the front end. When the post is submitted, the server validates this string of characters to ensure that the user submits the data in the form page returned by the server. Preventing someone from constantly submitting data to a url through an jquery script, for example, is a verification mechanism for data submission.
5. Effect.
You can see that the data submitted by the front end has been stored in the database.
The above is all the contents of the article "how to use Django to store the front-end data in Mysql database". 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.
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.