Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python Django in Development

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use python Django in development". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Use virtualenv to isolate the development environment

Using pip to manage project dependencies is mainly a trick, using pipfreeze > requirements.txt to save dependent modules and versions

Manage code base files using the .gitignore file provided by the gitignore.io website

Packaging and distribution

The Dockerfile of the Docker,Django project used for packaging and publishing the project is very simple:

FROMpython:3.5

COPY./requirements.txt/src

WORKDIR/src

RUNpipinstall-rrequirements.txt

COPY./src

EXPOSE

CMDuwsgi--http:--wsgi-file

This Dockerfile template can take 80% of the Django projects.

Log configuration

Now that you use Docker, you give up writing the log to a file and write directly to 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 collect logs from the Docker container directly.

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 test database, as well as memory-based cache, testing will not increase the dependence on other systems. Develop and get twice the result with half the effort.

In addition to writing automated test code, you should also be able to count test coverage. We are currently using coverage.py, which, to be honest, is not as good as node.js 's istanbul, and the output report is not as detailed and readable as Istanbul. But it's enough to check the "dead code".

Testing for http code

Some projects need to be docked with more third-party systems, such as Wechat authentication, payment, text messages and other common systems, and there may be other vertical business areas of systems. This part of the interface docking code, should also be included in the test, after all, Python as a scripting language, the code is prone to errors.

This block usually uses the responses module to mockhttp the request.

Scheduled task

There are some Django projects that need to do scheduled tasks. First of all, absolutely do not use linux's built-in crontab. The main problem is the high cost of maintenance, which may be forgotten one day.

Our current approach is to package scheduled tasks into a command with the help of the function of DjangoCommand. Run a scheduler in this command. It looks like this:

Importschedule

Fromdjango.core.management.baseimportBaseCommand

ClassCommand (BaseCommand):

Defhandle (self,*args,**kwargs):

Schedule.every (45) .minutes.do (do_this)

Schedule.every () .day.at ('04virtual 00') .do (do_that)

WhileTrue:

Schedule.run_pending ()

Time.sleep (1)

This is the end of the content of "how to use python Django in development". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report