検索対象: Two Scoops of Django Best Practices for Django 1.8
Beginner Python MateriaI Learn Python the Hard Way http://amzn. to/100YqYv If you don't know Python, this is one of the best places to start. FIhe author takes you through Python the same way he learned guitar, through rote and repetition. Don't worry about the title, this is a good way to get started with python. Learn Python the Hard Way On1ine Edition http: //learnpythonthehardway. org/ By going right to the source, this free for HTML, paid for video resources, is one of the best places tO start. "lhe video resources are especially useful. Automate the Boring Stuffwith python amazon . C0m/gP/Pr0duCt/1593275994 'lhis fascinating book teaches python by instructing on how to make boring computer tasks easy through automation. Why update 150 columns of a spreadsheet when python can dO it for you? UsefuI Python MateriaI Effective Python http://amzn. to/1NsiqVr lnstructs on many useful practices and techniqueswhen coding in python. Python C00 00k , 3rd Edition http://amzn . to/13Sv6q An incredible book Python luminaries David Beazley and Brian Jones, it's filled with delicious recipes... err... incredibly python recipes Python 3.3 or greater. Treading on Python VoIume 2 http://amzn. to/1kVWi2a Covers advanced python structures. 、 vriting ldiomatic Python 3.3 http://amzn . to/1aS5df4 Great tips for optimizing your code and increasing the legibility of your work. FIhere are a few places it differs 伝 om our practices (imports being the largest area ofdifference), but overall 、 concur. イ 67
Use Python 3.3.3 0 「 Later Django is a large, complicated system. 、 Mhile it's heavily tested for multiple versions of pyth on 3 , we've found that it just works better with more recent versions ofthe language. For example, mi g rate fails in curious ways with Python 3.3.0. Working With Python 2 and 3 We usually encounter this scenario when we are writing a third-party package for use in Django or even Just vanilla Python. However, there are use cases where an entire Django project might be deployed to Python 2.7 as well as python 3.3. Fortunately, most ofthe following suggestions 叩 p ツ no matter the scale of the project. Tests and ContinuousIntegration If there isn t a working test harness and functioning continuous integratl()n, no 、 is the time to set it up. Testing compatibility across major python versions simply requires automation. Keep CompatibiIity MinimaIIyInvasive %e last thing that a project needs is complex branches to deal with different versions ofPyth lherefore, use the following imports at the top of a python module to keep code identical: E 強 M も E 35 ・ 14 # The __future_— imports in this modute means that ・ a11 code in this exampte witt work identicalty in python 2.7 and に # Python 3 or higher. on. # Mu1ti —Line and Abs01ute/Retative imports work identicatty across # Python versions. from ___future import absolute_import # Any division witt return ftoat objects. Exampte / 2 = 1.5 from __future__ import division 475
C み 4 ア / 35. ・ / ア 臾 砌 市 % リ 防 切 g ″ ん ん 〃 3 When 、 do need more complexity or any sort OflogiC' that's 、 it s time tO create a ( 0 ア 4 ー ア ッ module. # A11 strings defined in python 2 and 3 can use python 3 name = tdjangot # syntax for defining unicode¯friendly strings ・ from __future—— importe Use django. utils. encoding. pythonü unicode-compatible in M0deIs Rather than write both —st r— ( ) and —uni code—( ) meth()d* use django. utils. encoding. python-2-unicode-compatibIe SO it only has to be written once. see section 19.3,'Viewing string Representations Of Objects. Resources lhe following are useful resources for Python 3 topics: Porting to Python 3 http://python3porting. com/ Lennart Regebro's free HTML or paid e-book bundle on the subject of moving 伝 om Python 2 to 3. Porting Django apps to Python 3 http: //youtu . be/cJMGvAYYUyY %is is Jacob Kaplan-Moss' PyCon US 2013 video on the subject. Python C00kb00 3rd Edition http: //amzn. t0/13Sv6q David BeazIey and Brian Jones' book of handy recipes for Python 3. 、 vriting ldiomatic Python 3.3 http://amzn. t0/1aS5df4 Jeff Knupp's guide to writing Python 3 code the'right' way ・ イ 76
C み 4 ア た r27 ゞ 12 ノ 4 〃 g & 冴 立 e• ・ 耘 ィ ー り 2 ん g お ( I 月 0 270 Django packages are downloadable 伝 om the PYthon Package lndex in addition to pip ・ release of Django, pip downloads the files 伝 om the python package lndex. Most PYthon and center for python package information and files. Whenever メ ou use pip to install a particular lhe python package lndex is much more than just a directory of it as the world's largest until it occurs on the Python Package lndex. For the vast majority ofPyth on community, open is 0 一 石 ci it lists over 55 , 000 packages, including Django itself• itory ofsoftware for the python programming language. As ofthe time this sentence was written' ThePythonPackageIndex(PyPI).Iocatedathttps://PYPi ・ python.org/pypi'isarepos- 21.2 Know About the Python Package lndex prefixed with django-", but there are many exceptions. ) of them in other python projects. (GenerallY' l)jango-specific packages generally have names Note that not 砠 of those packages are l)jango-specifi% which means that you can use some packages to consider adding to your projects ・ out Two scoops of Django. lhis list is a great starting point ifyou're looking for highly-useful Appendix A: ル c ん g お イ 砌 石 記 ル な ル covers all of the packages mentioned through- 21.1 ExampIes 0fThird-Party Packages Figure 21.2 : 第 secret is out. lt's just hot fudge.
Appendix F: Working with Python 3 l)jango's 0 ー 五 ci documentation has a well-written page that covers much of what's involved with working with Python 3. Here are some specific sections you should pay careful attention to : DjangoOfficialDocsonPython3 http://2scoops. C0/1.8-python3 CodingGuidelines http://2scoops. C0/1.8-python3-coding-guideIines 、 VritingCompatibIeCodewithSix http://2scoops. C0/1.8-python3-six Here are some other things you should ow. Most CriticaI Packages Work with Python 3 As of the time of this writing, Django packages lists over 205 packages that support python 3. %is includes such critical libraries as: Django itself Pi110w django—braces django-crispy-forms django-debug-toolbar django-floppyforms django-rest-framework python-requests youcanseealistofl)jangospecificlibrariesathttps: //www. djangopackages.com/python3/. 473
C ろ 4 ア / 35. ・ / ア ア 砌 イ ⅸ F• ・ 曻 房 を 〃 gw 2 ん 〃 3 Checking f0 「 Python 3 Compatibility Here are the steps we 応 Ⅱ ow in rough order when determining if a third-party library actually works with Python 3 : Checkon https://www.djangopackages. com/PYthon3/. Look up the package on pyPl and see if any of its trove classifiers mention Python 3 status. See if a pull request for python 3 support is outstanding. Run the test suite using Python 3.4. If a Django project, check the models for —st r— ( ) methods. If it has them' it's a pretty good indicator that it's Python 3.4 friendly. Converting a Library t0 Workwith PYthon 3.4 How we convert Python 2 code to Python 3 : Get the test harness working with Python 3. Lean on dj ango ・ contri b. si x as much as possible. Add a ( 0 襯 ア 可 ・ カ , module only if absolutely needed. Fix any problems you find in the code. Try to keep solutions as simple as possible. S ubmit the pull request ・ politely poke the package owner to accept the pull request. Once the owner accepts the pull request' gently poke the package owner to push the update to PyPI. TIP: DeaIing with ow Maintainers Ranting tO or complaining about S10 、 M-movmg maintamers is absolutely counter- productive. people have lives and jobs that sometimes get in the way 0f open source ・ lt's more productive t0 be patient' be polit% and if necessary d0 an absolutely minimal fork or find a working alternative. イ 74
Appendix D: Localization lnternationalization and Django and Python provides a 10t of very useful t001S for dealing with internationalization, localization , and of course, Unicode. FIhis appendix, added as of the second edition, contains a list of things helpful for preparing your Django application for non-EngIish readers and non-USA users. FIhis list is by no means complete, and we invite the reader to provide additional feedback. Start EarIy lt is always easier to start with and grow an internationalized, localized project than to convert an existing project ・ Define Python Source Code Encodings ln PEP 263 we are given a formal specification for defining how encoding of python modules is to occur. Amongst other things, this affects how python handles unicode literals. To define this encoding in internationalized projects, at the top each module add: EXAMPLE 35 、 9 # ー ☆ ー coding: utf—8 ー 大 一 Or as shown in the next code example: 463