Home > Article > Web Front-end > Summarize the experience of using python Django in development
This time I will bring you a summarypython DjangoThe experience of using Django in development, what are the notes in python Django development, as follows This is a practical case, let’s take a look at it.
I first came into contact with Django when I was a junior in college. It has been almost 4 years since I actually used Django to do projects. My favorite is actually Django’s ORM framework. The company's projects are separated from front to back, and it is very efficient to use Django for back-end interface development.
Hereby summarize some experiences in Django development. Let’s talk about some of the most basics first.
Use virtualenv to isolate the development environment
Using pip to manage project dependencies is mainly a little trick. Use pip freeze > requirements.txt to save dependent modules and Version
Use the .gitignore file provided by gitignore.io to manage the code base file
Packaging and publishing
Packaging and publishing of the projectDocker, The Dockerfile of the Django project is very simple:
FROM python:3.5 COPY ./requirements.txt /src WORKDIR /src RUN pip install -r requirements.txt COPY . /src EXPOSE CMD uwsgi --http :--wsgi-file<path/to/wsgi.py>
This Dockerfile template can cover 80% of the Django projects.
Log configuration
Since I use Docker, I have given up writing the log to the file and written it directly to the standard output.
# settings.py # ... LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': '[application] %(levelname)s %(asctime)s %(module)s %(message)s' } }, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'stream': sys.stdout, 'formatter': 'verbose' }, }, 'loggers': { 'app': { 'handlers': ['console'], 'level': 'DEBUG', 'propagate': True, }, }, }
The new version of uwsgi can already collect webapp logs and output them to standard output. If you need to collect and manage logs, you can use the Docker log collection tool to directly collect the logs of the Docker container.
Automated testing
Since it is a pure back-end project, engineers can test their own code through automated testing. Django itself provides good support for testing. You can use sqlite to build a test database and memory-based cache. Testing will not increase dependence on other systems. Develop with less effort.
In addition to writing automated test code, you must also be able to count test coverage. Currently, we are using the tool coverage.py. To be honest, it is not as easy to use as istanbul of node.js, and the output report is not as detailed and easy to read as Istanbul. However, it is still useful for checking "dead code".
Testing for http code
Some projects need to connect to more third-party systems, such as WeChat authentication, payment, SMS and other common systems, and there may be systems in other vertical business fields. This part of the interface docking code should also be included in the test. After all, Python is a scripting language and the code is prone to errors.
This section generally uses the responses module to mock http requests.
Timing tasks
There are some Django projects that need to do some timing tasks. First of all, never use Linux’s built-in crontab. The main problem is that the maintenance cost is high, and this configuration may be forgotten one day.
Our current method is to use the function of Django Command to encapsulate scheduled tasks into a command. Run a scheduler in this command. Just like the following:
import schedule from django.core.management.base import BaseCommand class Command(BaseCommand): def handle(self, *args, **kwargs): schedule.every(45).minutes.do(do_this) schedule.every().day.at('04:00').do(do_that) while True: schedule.run_pending() time.sleep(1)
If you have any questions about this, you can ask me at any time. I have studied deeply about learning methods, systematic learning planning, and learning efficiency. I hope you can Help everyone avoid detours. The top three in the Python newbie exchange group are: 463, the middle three are: 024, and the last three are: 091
I believe you have mastered the method after reading these cases. Please pay attention for more exciting things. Other related articles on php Chinese website!
Related reading:
##Solution to the gap between the image and view tags
Why slots are used in subcomponents
How to use getBoundingClientRect() to achieve scrolling and fixation of div containers
The above is the detailed content of Summarize the experience of using python Django in development. For more information, please follow other related articles on the PHP Chinese website!