Step Python3 (13)--0--django Form forms

Source: Internet
Author: User

Django has provided us with form validation, so let's take a look at the following: Wu Sir Blog: http://www.cnblogs.com/wupeiqi/articles/5246483.html
After you create the Django project, create the app.
Configure Urls:
  
 
  1. from django.conf.urls import url
  2. from django.contrib import admin
  3. from myapp01 import views
  4. urlpatterns = [
  5. url(r‘^admin/‘, admin.site.urls),
  6. url(r‘^login/‘, views.login),
  7. ]
Configuration views (app01.views):
  
 
  1. from django.shortcuts import render
  2. from django import forms
  3. # Create your views here.
  4. class InputForm(forms.Form):
  5. # 下面使用的变量名必须和html中input标签的name值相同
  6. username = forms.CharField(required=True)
  7. password = forms.CharField(required=True)
  8. def login(request):
  9. if request.method == ‘POST‘:
  10. obj = InputForm(request.POST)
  11. ret = obj.is_valid()
  12. print(ret,obj.data)
  13. if ret:
  14. print(obj.clean())
  15. else:
  16. print(obj.errors)
  17. return render(request, ‘login.html‘)

Configuration tamplates (index.html):
  
 
  1. <!DOCTYPE html>
  2. lang="en">
  3. <meta charset="UTF-8">
  4. <title>Title</title>
  5. <body>
  6. <div>
  7. <div>
  8. <input type="text" name="username" />
  9. </div>
  10. <div>
  11. <input type="password" name="password" />
  12. </div>
  13. <input type="button" value="提交" onclick="DoSubmit();"/>
  14. </div>
  15. <script src="/static/jquery-2.1.4.min.js"></script>
  16. <script>
  17. function DoSubmit() {
  18. var input_dic = {};
  19. $(‘input‘).each(function () {
  20. var v = $(this).val();
  21. var n = $(this).attr(‘name‘);
  22. input_dic[n] = v;
  23. });
  24. console.log(input_dic);
  25. $.ajax({
  26. url:‘/login/‘,
  27. type:‘POST‘,
  28. data:input_dic,
  29. success: function (rdata) {
  30. console.log(rdata);
  31. },
  32. error: function () {
  33. }
  34. })
  35. }
  36. </script>
  37. </body>

You can get the input after you access http://127.0.0.1:8000/login/commit.



Null

Step Python3 (13)--0--django Form forms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.