In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the use of python Django framework to achieve web application example analysis, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
1. For the production environment of django+python27+mysql, what should I pay attention to if I want to change it to python3? Production environment data do not want to be destroyed, so do not dare to do it easily, hope God to give some advice?
About python upgrade:
You can use Python3's native tool, 2to3, to convert the code of python2 into python3. A small part of the code still needs to be converted manually.
After upgrading the python version, the version of the third-party package needs to be upgraded. Some third-party packages do not support python3. At this point, you can try to use the 2to3 tool to transform the python source code of the third package, and then use it. There is a certain risk. Can not be converted, need to find a replacement package, the corresponding code on the application server needs to be modified.
About django upgrade:
Django1.5 began to support python3. And django1.11 is the last version that supports python2. If you use a django version greater than or equal to 1.5, the django version may not be upgraded. If the django version is less than 1.5, you need to upgrade the django version. After upgrading the django version, the old API that the new version is not compatible with needs to be modified. This workload is relatively large.
After upgrading the django version, third-party packages for django services need to be upgraded accordingly. It is recommended that django do not use the latest version immediately, lest third-party packages do not support the latest django version.
About mysql:
Mysqldb does not support python3, so you need to use mysqlclient as a database API driver.
Django2.0 only supports myslq5.5 or later
General considerations for version upgrade.
Back up the code and data
After python and django upgrades, full testing is required before launching.
Click to add picture description (60 words maximum)
2. I just learned the nginx deployment of django, but I still don't know the principle of dealing with high concurrency. Can you explain it?
Let me briefly state my understanding.
Django WSGI is single-threaded, meaning that one request cannot respond to another until it has been processed.
Compared with using Django alone, uWSGI (C language writing) + Django has the advantage of multi-core through multi-process and supports higher concurrency.
The web client the web server the socket uwsgi Django
The uwsgi server runs on the django side.
Compared with using Django alone, uWSGI (writing in C language) + Django uses multi-processes, gives full play to the advantages of multi-core and supports higher concurrency. There are master and worker processes in uWSGI. The master process creates the appropriate number of worker processes according to the configuration. After a worker process is created, it runs an instance of the python interpreter and then uses it until the worker process is destroyed. Master assigns received http requests to worker processing. The worker process invokes the python script to process the http request. A worker process is used repeatedly to process many HTTP requests. The number of woker processes should be greater than the number of CPU cores. Then increase the number of processes based on CPU utilization.
3, I now the system needs a function, when a task reaches a certain state, if there is no one to operate 24 hours, then automatically switch to another state, for each user is a different task, a little like Baidu takeout orders waiting for merchants to take orders of the kind, what can facilitate the integration of django framework? What about django-celery?
Let's briefly talk about my consideration for your reference.
After the user places the order, the user order information is written to the database. After the merchant receives the order, modify the user order status in the database.
About timers: consider using django-celery to create a thread for asynchronous processing and check the status of user orders in the database 24 hours later. If there is still no merchant to take the order, modify the order status. But there may be too many threads and too much resource consumption. You can use django-celery to set up circular tasks, such as querying orders in the database that have been in the database for more than 24 hours every 5 minutes, but still no merchant has received orders, and modify the status of these orders.
4. How to run the program in Django? For the sake of the competition, to explain the software in a Django framework, I studied java, but have not come into contact with the framework of Python? Where is the entrance? How does it work?
This is found in my tutorial "Python Django takes you to build Web applications".
Enter the django project root directory. Python manage.py runserver runs the built-in Web server. The default IP address for the service is 127.0.0.1 and the port is 8000.
If you want to provide services, you can change it to a real IP address, for example, you want to use 1.1.1.1 8080 to provide services. Then modify the content of ALLOWED_HOSTS in settings.py as ALLOWED_HOSTS = ['1.1.1.1,]. D:\ django\ web > python manage.py runserver 1.1.1.1 python manage.py runserver 8000 runs the built-in Web server.
5. I would like to ask you a question: can you use django as a travel survey website? is it feasible to investigate the travel intentions of your classmates, store the information in the database, and then make a simple analysis to get the best tourist destination?
In the first part, the requirements for collecting and storing information are related to django. This is a simple Web application. Use the form to get the students' travel intentions. Because of the small amount of data, you can store the data directly in the SQLite database.
The second part of the data analysis and results presentation, do not necessarily need to rely on django. Extract information from the database for analysis as needed. If the analysis is complex, you can use numpy, pandas for analysis, and use matploitlib to show the analysis results.
6, originally intended to develop a web application, mainly the website and client (pda) access, now the website is very convenient to use django to complete, that in the client (pda) above the idea is to use webservice, do not know that you can directly use django to achieve. Can we do it directly with django without webservice, hoping to get a solution and popularize the knowledge?
You can use Django REST framework to implement RESTful web service.
7. How to use django for rapid development most efficiently? Or can it be said that the parts of django can help achieve rapid development?
I think the reason for the rapid development of django is:
First of all, python, as a dynamically typed language, is flexible in use and fast in development.
Second, compare it with flask, another commonly used python web framework. Django provides more comprehensive features. Django is a heavyweight framework. Flask is a lightweight framework, many basic functions need to be implemented by themselves or use third-party components, but the use of flexible.
Django advantages: built-in features, powerful (such as forms, authentication, access control, file upload, testing, internationalization, paging, serialization, session, data validation, exception handling) easy to use, many functions do not need programmers to achieve their own, powerful ORM mechanism, powerful admin site, url pattern flexible and easy to use, APP pluggable, error messages are very detailed, official documentation is complete.
Disadvantages of django: there are many layers of encapsulation and it is difficult to modify. It is difficult to replace built-in components with your favorite third-party components. Django is weak in order to separate presentation from logic, so DTL is relatively weak.
Select the framework according to the requirements, select django for rapid development, customize deeply, and choose flask.
8. When I use the terminal to create a django project, I am prompted to django-admin: command not found, but it is OK to run Python to import django. What went wrong and it has been created successfully before? can you help me answer it?
For example, if the python installation directory is D:\ Python36\, then after installing django, django-admin.exe is in D:\ Python36\ Scripts\. You need to add D:\ Python36\ Scripts\; to the system variable Path.
9. I would like to ask, for a grouping query, there is now a "gender" field. I want to count how many boys and girls there are. I want to use the method of grouping query. But can't find the sample code, ask the teacher for advice?
Similar query how many blog posts have been written by each user
From django.db.models import Count
Numbers = Blog.objects.values ('username') .annotate (blog_count=Count (' username')) .order_by ()
Equivalent to SQL statement: select username, count (*) from Blog group by username
The resulting value of QuerySets numbers is:
Finally, order_by () is added because class meta: ordering is defined in model.
10. Is the general view in Django a difficulty? I have been working on this general view all day today, but I don't understand it at all. No detailed information can be found on the Internet. I would like to ask the teacher for a brief introduction.
The most common thing the django view does is to get data from the data based on the parameters passed in the url. Load the template and return to the rendered template. Django provides a general view to implement this common process. This reduces the amount of code for the view.
The name of the parameter in URLconf may need to be changed, for example, the DetailView generic view requires the use of the parameter "contention" to represent the primary key.
In a view class that inherits a common view class, you only need to specify model and template_name to complete the basic process.
Also note that the context variable corresponds to the template.
Django2.0 's manual 2.6.2 Use generic views: Less code is better gives a basic example that you can try.
11. I used to work on a project: because the tables are not associated, I use a non-relational database mongodb, and the project development environment uses python+django and is deployed on ubuntu. At that time, pymongo was used to connect python and mongodb, and mongoengine was used to connect django and mongodb.
Now there are new requirements, and there are associations between tables, so I'm going to add a mysql database.
I would like to ask the teacher, when django displays the web page, can you display the contents of the table in mongodb and the table in mysql at the same time? How should the database be configured in settings.py?
You can display data from both Mongodb and mysql on the same web page. In Django, you only need to pass what needs to be displayed to the template through context. No matter what kind of database the context content comes from.
Add the parameters required to connect to the mysql in setting.py DATABASES, such as:
'db2': {
'ENGINE': 'django.db.backends.mysql'
'NAME': 'blog'
'USER': 'root'
'PASSWORD': '123456'
'HOST': '10.0.0.1'
'PORT': '3306'
}
You can set the mapping relationship between database and app
DATABASE_APPS_MAPPING = {
'app1': 'db1'
'app2': 'db2'
}
You can also use the function using () or the parameter using to specify which database to use when querying or modifying the contents of the database.
12. As a rookie, I would like to ask the great god: it is said that python django is relatively easy to learn. How long does it take from entry to learning? That is, how long is the study cycle? What foundations are needed?
If you understand the MVC framework and http protocol, but also understand python, it is faster to learn django. Otherwise, it will take longer.
13. I automatically created the tables needed in the background with django. One of the tables An is used to store the news content that needs to be displayed. Before, the news content was stored in another table B (mysql's database code was latin, later changed to utf8,A table B table is all utf8 code). I imported the contents of table B into table A, the English part is normal, but the Chinese part is all changed into a question mark, solve?
The above is a sample analysis of web applications using the python Django framework. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.