In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to configure Django". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to configure Django.
1. Preparatory work
Install using pip: pip install Django
two。 Basic configuration
1) create a django program
a. Terminal commands: when django-admin startproject mysite and IDE create django programs, they essentially execute the above commands automatically.
2) the directory structure is as follows:
3) configuration file-- (settings.py)
a. Database
b. Template
c. Static file
3. Create App
a. Command
Python manage.py startapp cmdb
B. The cmdb directory structure is as follows:
4. Log in to the instance
A. generate html files, such as login.html, in the templates directory
Log in
Male: female:
b. Modify the url file to define routing rules
From django.contrib import adminfrom django.conf.urls import urlfrom cmdb import viewsurlpatterns = [url (r'^ admin/', admin.site.urls), url (r'^ login/', views.login),]
c. Define the view function: views.py under app
From django.shortcuts import renderfrom django.shortcuts import HttpResponsefrom django.shortcuts import redirect# Create your views here.USER_LIST = {} def login (request): if request.method = = 'GET': # determine the request method return render (request,' login.html') elif request.method = = 'POST': user = request.POST.get (' user') # post request Select and input box to get the value pwd = request.POST.get ('pwd') sex = request.POST.get (' sex') if user and pwd: USER_LIST ['name'] = user USER_LIST [' pwd'] = pwd USER_LIST ['sex'] = sex return render (request,' success.html' {"user_list": USER_LIST}) else: return HttpResponse ('request cannot be empty') else: return HttpResponse ('request method is not get\ post') # HttpResponse ("string")
d. After the submission is successful, the success.html page takes the value, and the hmtl template obtains the dictionary data
Login success page {{user_list.name}} # get the value of a key in the dictionary {% for k department v in user_list.items%} # Circular dictionary {{k}: {{v}} {% endfor%}
e. Access through the browser: http://127.0.0.1:8000/login/, display the login.html login page, enter login information, after the login is successful, the data obtained is as follows
Summary: from the above example, we can know the life cycle of django:
-> URL correspondence (match)-- > View function-> return user string
-> URL correspondence (match)-> View function-- > Open a HTML file and read the contents
5. Other
Request.GET.get (', None) # get the data sent by get request request.POST.get (', None) # get the data sent by post request return render (request, 'path of HTML template') return redirect ('only enter URL')
How to get the value of the corresponding multi-check box and uploaded file?
a. Check box, the html format is as follows:
Multiple= "multiple" means multiple selections
Beijing, Shanghai and Shenzhen
Views view page:
City = request.POST.getlist ('city')
Page visit result:
b. Upload files
Main methods
File object = request.FILES.get ('xx') file name = file object .name file size = file object .size file content = file object .chunks ()
Html format, form form needs to add attributes:
Enctype= "multipart/form-data"
Views view:
# upload file upload_file_obj = request.FILES.get ('upload') print (type (upload_file_obj), upload_file_obj) # 2.jpg # Save the uploaded file to the upload directory upload_path = os.path.join (' upload', upload_file_obj.name) fw = open (upload_path 'wb') for line in upload_file_obj.chunks (): # chunks represents all databases Is an iterator fw.write (line) fw.close ()
Page visit result:
The final views code:
From django.shortcuts import renderfrom django.shortcuts import HttpResponsefrom django.shortcuts import redirectimport os# Create your views here.USER_LIST = {} def login (request): if request.method = = 'GET': # determine the request method return render (request,' login.html') elif request.method = = 'POST': user = request.POST.get (' user') # post request Single selection, input box to get the value pwd = request.POST.get ('pwd') sex = request.POST.get (' sex') # multiple selection value city = request.POST.getlist ('city') # upload file upload_file_obj = request.FILES.get (' upload') print (type (upload_file_obj) Upload_file_obj) # 2.jpg # Save the uploaded file to the upload directory upload_path = os.path.join ('upload', upload_file_obj.name) fw = open (upload_path,' wb') for line in upload_file_obj.chunks (): # chunks represents all databases It is an iterator fw.write (line) fw.close () if user and pwd: USER_LIST ['name'] = user USER_LIST [' pwd'] = pwd USER_LIST ['sex'] = sex USER_LIST [' city'] = city USER_LIST ['file'] = upload_file_obj.name return render (request 'success.html', {"user_list": USER_LIST}) else: return HttpResponse (' request cannot be empty') else: return HttpResponse ('request method is not get\ post') # HttpResponse ("string") 6. URL based on regular expression
Simple examples are as follows:
a. Modify the url file to define routing rules
Urlpatterns = [# url (r'^ admin/', admin.site.urls), # url (r'^ login/', views.login), url (r'^ index/', views.index), url (r'^ detail/', views.detail),]
b. View views
USER_DICT = {"1": {"name": "root1", "email": "qwe1@163.com"}, "2": {"name": "root2", "email": "qwe2@163.com"}, "3": {"name": "root3", "email": "qwe3@163.com"}, "4": {"name": "root4" "email": "qwe4@163.com"},} def index (request): return render (request, 'index.html', {"user_dict": USER_DICT})
c. Template index.html, circular dictionary information
Click on any information to jump to the detail?nid=x page for more information
Index {% for for v in user_dict.items%} {{v.name}} {% endfor%}
d. The browser accesses http://127.0.0.1:8000/index/ and the page is as follows:
e. Click on any information on the page to jump to the detail detail page, the views view of the detail page
Def detail (request): nid = request.GET.get ('nid') # get request method, or value to nid, that is, key detail_info of USER_DICT = USER_ DICT [nid] return render (request,' detail.html', {"detail_info": detail_info})
F. The detail.html template information is as follows:
Details page details user name: {{detail_info.name}} email: {{detail_info.email}}
g. Click the root2 hyperlink and jump to another page, as follows:
7. Optimization of a kind of url method
The get request of the above detail page is in the form of http:127.0.0.1:80/detail?nid=x, while that of nid is obtained dynamically. If you want to change the method to compare low, you can change it to the following:
Http://127.0.0.1:80/detail-x.html
A. the optimal writing method of url
The optimized Writing method of b.views
C. optimized writing method of html template
d. Browse page access effect
8. If the urls has been changed, then the urls involved also needs to be changed, and after many changes, there may be omissions in the modification. Is there any efficient solution?
a. Find the urls.py file and modify the routing rules
# url (ringing detailUniverse, views.detail), # url (ringing detail- (\ d +) .html /', views.detail), # regular expression url (ringing detail- (\ d +) .html /', views.detail, name='detail_name'), # regular expression
B, find the views file and modify it
Def index (request): # return HttpResponse ('index') return render (request,' index.html', {"user_dict": USER_DICT}) # def detail (requerst): # nid = requerst.GET.get ('nid') # detail_info = USER_ DICT [nid] # return render (requerst,' detail.html', {"detail_info": detail_info}) def detail (requerst Nid): # nid specifies the content detail_info = USER_ DICT [nid] return render (requerst, 'detail.html', {"detail_info": detail_info}) in (\ d +)
c. Index.html under the templates directory
Dictionary cycle to get {{user_dict.KEY1} {% for KJV in user_dict.items%} {{v.name}} {% endfor%} so far, I believe you have a deeper understanding of "how to configure Django". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.