contributing.txt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. ===================================
  2. Writing your first patch for Django
  3. ===================================
  4. Introduction
  5. ============
  6. Interested in giving back to the community a little? Maybe you've found a bug
  7. in Django that you'd like to see fixed, or maybe there's a small feature you
  8. want added.
  9. Contributing back to Django itself is the best way to see your own concerns
  10. addressed. This may seem daunting at first, but it's really pretty simple.
  11. We'll walk you through the entire process, so you can learn by example.
  12. Who's this tutorial for?
  13. ------------------------
  14. .. seealso::
  15. If you are looking for a reference on how to submit patches, see the
  16. :doc:`/internals/contributing/writing-code/submitting-patches`
  17. documentation.
  18. For this tutorial, we expect that you have at least a basic understanding of
  19. how Django works. This means you should be comfortable going through the
  20. existing tutorials on :doc:`writing your first Django app</intro/tutorial01>`.
  21. In addition, you should have a good understanding of Python itself. But if you
  22. don't, `Dive Into Python`__ is a fantastic (and free) online book for
  23. beginning Python programmers.
  24. Those of you who are unfamiliar with version control systems and Trac will find
  25. that this tutorial and its links include just enough information to get started.
  26. However, you'll probably want to read some more about these different tools if
  27. you plan on contributing to Django regularly.
  28. For the most part though, this tutorial tries to explain as much as possible,
  29. so that it can be of use to the widest audience.
  30. .. admonition:: Where to get help:
  31. If you're having trouble going through this tutorial, please post a message
  32. to |django-developers| or drop by `#django-dev on irc.freenode.net`__ to
  33. chat with other Django users who might be able to help.
  34. __ https://diveinto.org/python3/table-of-contents.html
  35. __ irc://irc.freenode.net/django-dev
  36. What does this tutorial cover?
  37. ------------------------------
  38. We'll be walking you through contributing a patch to Django for the first time.
  39. By the end of this tutorial, you should have a basic understanding of both the
  40. tools and the processes involved. Specifically, we'll be covering the following:
  41. * Installing Git.
  42. * Downloading a copy of Django's development version.
  43. * Running Django's test suite.
  44. * Writing a test for your patch.
  45. * Writing the code for your patch.
  46. * Testing your patch.
  47. * Submitting a pull request.
  48. * Where to look for more information.
  49. Once you're done with the tutorial, you can look through the rest of
  50. :doc:`Django's documentation on contributing</internals/contributing/index>`.
  51. It contains lots of great information and is a must read for anyone who'd like
  52. to become a regular contributor to Django. If you've got questions, it's
  53. probably got the answers.
  54. .. admonition:: Python 3 required!
  55. The current version of Django doesn't support Python 2.7. Get Python 3 at
  56. `Python's download page <https://www.python.org/downloads/>`_ or with your
  57. operating system's package manager.
  58. .. admonition:: For Windows users
  59. When installing Python on Windows, make sure you check the option "Add
  60. python.exe to Path", so that it is always available on the command line.
  61. Code of Conduct
  62. ===============
  63. As a contributor, you can help us keep the Django community open and inclusive.
  64. Please read and follow our `Code of Conduct <https://www.djangoproject.com/conduct/>`_.
  65. Installing Git
  66. ==============
  67. For this tutorial, you'll need Git installed to download the current
  68. development version of Django and to generate patch files for the changes you
  69. make.
  70. To check whether or not you have Git installed, enter ``git`` into the command
  71. line. If you get messages saying that this command could not be found, you'll
  72. have to download and install it, see `Git's download page`__.
  73. If you're not that familiar with Git, you can always find out more about its
  74. commands (once it's installed) by typing ``git help`` into the command line.
  75. __ https://git-scm.com/download
  76. Getting a copy of Django's development version
  77. ==============================================
  78. The first step to contributing to Django is to get a copy of the source code.
  79. First, `fork Django on GitHub <https://github.com/django/django/fork>`__. Then,
  80. from the command line, use the ``cd`` command to navigate to the directory
  81. where you'll want your local copy of Django to live.
  82. Download the Django source code repository using the following command:
  83. .. console::
  84. $ git clone git@github.com:YourGitHubName/django.git
  85. .. admonition:: Low bandwidth connection?
  86. You can add the ``--depth 1`` argument to ``git clone`` to skip downloading
  87. all of Django's commit history, which reduces data transfer from ~250 MB
  88. to ~70 MB.
  89. Now that you have a local copy of Django, you can install it just like you would
  90. install any package using ``pip``. The most convenient way to do so is by using
  91. a *virtual environment*, which is a feature built into Python that allows you
  92. to keep a separate directory of installed packages for each of your projects so
  93. that they don't interfere with each other.
  94. It's a good idea to keep all your virtual environments in one place, for
  95. example in ``.virtualenvs/`` in your home directory.
  96. Create a new virtual environment by running:
  97. .. console::
  98. $ python3 -m venv ~/.virtualenvs/djangodev
  99. The path is where the new environment will be saved on your computer.
  100. The final step in setting up your virtual environment is to activate it:
  101. .. code-block:: console
  102. $ source ~/.virtualenvs/djangodev/bin/activate
  103. If the ``source`` command is not available, you can try using a dot instead:
  104. .. code-block:: console
  105. $ . ~/.virtualenvs/djangodev/bin/activate
  106. You have to activate the virtual environment whenever you open a new
  107. terminal window. virtualenvwrapper__ is a useful tool for making this
  108. more convenient.
  109. .. admonition:: For Windows users
  110. To activate your virtual environment on Windows, run:
  111. .. code-block:: doscon
  112. ...\> %HOMEPATH%\.virtualenvs\djangodev\Scripts\activate.bat
  113. or you can install :ref:`a Windows version of virtualenvwrapper
  114. <virtualenvwrapper-win>` and then use:
  115. .. code-block:: doscon
  116. ...\> workon djangodev
  117. __ https://virtualenvwrapper.readthedocs.io/en/latest/
  118. The name of the currently activated virtual environment is displayed on the
  119. command line to help you keep track of which one you are using. Anything you
  120. install through ``pip`` while this name is displayed will be installed in that
  121. virtual environment, isolated from other environments and system-wide packages.
  122. Go ahead and install the previously cloned copy of Django:
  123. .. console::
  124. $ python -m pip install -e /path/to/your/local/clone/django/
  125. The installed version of Django is now pointing at your local copy. You will
  126. immediately see any changes you make to it, which is of great help when writing
  127. your first patch.
  128. Running Django's test suite for the first time
  129. ==============================================
  130. When contributing to Django it's very important that your code changes don't
  131. introduce bugs into other areas of Django. One way to check that Django still
  132. works after you make your changes is by running Django's test suite. If all
  133. the tests still pass, then you can be reasonably sure that your changes
  134. work and haven't broken other parts Django. If you've never run Django's test
  135. suite before, it's a good idea to run it once beforehand to get familiar with
  136. its output.
  137. Before running the test suite, install its dependencies by ``cd``-ing into the
  138. Django ``tests/`` directory and then running:
  139. .. console::
  140. $ python -m pip install -r requirements/py3.txt
  141. If you encounter an error during the installation, your system might be missing
  142. a dependency for one or more of the Python packages. Consult the failing
  143. package's documentation or search the Web with the error message that you
  144. encounter.
  145. Now we are ready to run the test suite. If you're using GNU/Linux, macOS, or
  146. some other flavor of Unix, run:
  147. .. console::
  148. $ ./runtests.py
  149. Now sit back and relax. Django's entire test suite has thousands of tests, and
  150. it takes at least a few minutes run, depending on the speed of your computer.
  151. While Django's test suite is running, you'll see a stream of characters
  152. representing the status of each test as it completes. ``E`` indicates that an
  153. error was raised during a test, and ``F`` indicates that a test's assertions
  154. failed. Both of these are considered to be test failures. Meanwhile, ``x`` and
  155. ``s`` indicated expected failures and skipped tests, respectively. Dots indicate
  156. passing tests.
  157. Skipped tests are typically due to missing external libraries required to run
  158. the test; see :ref:`running-unit-tests-dependencies` for a list of dependencies
  159. and be sure to install any for tests related to the changes you are making (we
  160. won't need any for this tutorial). Some tests are specific to a particular
  161. database backend and will be skipped if not testing with that backend. SQLite
  162. is the database backend for the default settings. To run the tests using a
  163. different backend, see :ref:`running-unit-tests-settings`.
  164. Once the tests complete, you should be greeted with a message informing you
  165. whether the test suite passed or failed. Since you haven't yet made any changes
  166. to Django's code, the entire test suite **should** pass. If you get failures or
  167. errors make sure you've followed all of the previous steps properly. See
  168. :ref:`running-unit-tests` for more information.
  169. Note that the latest Django master may not always be stable. When developing
  170. against master, you can check `Django's continuous integration builds`__ to
  171. determine if the failures are specific to your machine or if they are also
  172. present in Django's official builds. If you click to view a particular build,
  173. you can view the "Configuration Matrix" which shows failures broken down by
  174. Python version and database backend.
  175. __ https://djangoci.com
  176. .. note::
  177. For this tutorial and the ticket we're working on, testing against SQLite
  178. is sufficient, however, it's possible (and sometimes necessary) to
  179. :ref:`run the tests using a different database
  180. <running-unit-tests-settings>`.
  181. Working on a feature
  182. ====================
  183. For this tutorial, we'll work on a "fake ticket" as a case study. Here are the
  184. imaginary details:
  185. .. admonition:: Ticket #99999 -- Allow making toast
  186. Django should provide a function ``django.shortcuts.make_toast()`` that
  187. returns ``'toast'``.
  188. We'll now implement this feature and associated tests.
  189. Creating a branch for your patch
  190. ================================
  191. Before making any changes, create a new branch for the ticket:
  192. .. console::
  193. $ git checkout -b ticket_99999
  194. You can choose any name that you want for the branch, "ticket_99999" is an
  195. example. All changes made in this branch will be specific to the ticket and
  196. won't affect the main copy of the code that we cloned earlier.
  197. Writing some tests for your ticket
  198. ==================================
  199. In most cases, for a patch to be accepted into Django it has to include tests.
  200. For bug fix patches, this means writing a regression test to ensure that the
  201. bug is never reintroduced into Django later on. A regression test should be
  202. written in such a way that it will fail while the bug still exists and pass
  203. once the bug has been fixed. For patches containing new features, you'll need
  204. to include tests which ensure that the new features are working correctly.
  205. They too should fail when the new feature is not present, and then pass once it
  206. has been implemented.
  207. A good way to do this is to write your new tests first, before making any
  208. changes to the code. This style of development is called
  209. `test-driven development`__ and can be applied to both entire projects and
  210. single patches. After writing your tests, you then run them to make sure that
  211. they do indeed fail (since you haven't fixed that bug or added that feature
  212. yet). If your new tests don't fail, you'll need to fix them so that they do.
  213. After all, a regression test that passes regardless of whether a bug is present
  214. is not very helpful at preventing that bug from reoccurring down the road.
  215. Now for our hands-on example.
  216. __ https://en.wikipedia.org/wiki/Test-driven_development
  217. Writing a test for ticket #99999
  218. --------------------------------
  219. In order to resolve this ticket, we'll add a ``make_toast()`` function to the
  220. top-level ``django`` module. First we are going to write a test that tries to
  221. use the function and check that its output looks correct.
  222. Navigate to Django's ``tests/shortcuts/`` folder and create a new file
  223. ``test_make_toast.py``. Add the following code::
  224. from django.shortcuts import make_toast
  225. from django.test import SimpleTestCase
  226. class MakeToastTests(SimpleTestCase):
  227. def test_make_toast(self):
  228. self.assertEqual(make_toast(), 'toast')
  229. This test checks that the ``make_toast()`` returns ``'toast'``.
  230. .. admonition:: But this testing thing looks kinda hard...
  231. If you've never had to deal with tests before, they can look a little hard
  232. to write at first glance. Fortunately, testing is a *very* big subject in
  233. computer programming, so there's lots of information out there:
  234. * A good first look at writing tests for Django can be found in the
  235. documentation on :doc:`/topics/testing/overview`.
  236. * Dive Into Python (a free online book for beginning Python developers)
  237. includes a great `introduction to Unit Testing`__.
  238. * After reading those, if you want something a little meatier to sink
  239. your teeth into, there's always the Python :mod:`unittest` documentation.
  240. __ https://www.diveinto.org/python3/unit-testing.html
  241. Running your new test
  242. ---------------------
  243. Since we haven't made any modifications to ``django.shortcuts`` yet, our test
  244. should fail. Let's run all the tests in the ``shortcuts`` folder to make sure
  245. that's really what happens. ``cd`` to the Django ``tests/`` directory and run:
  246. .. console::
  247. $ ./runtests.py shortcuts
  248. If the tests ran correctly, you should see one failure corresponding to the test
  249. method we added, with this error::
  250. ImportError: cannot import name 'make_toast' from 'django.shortcuts'
  251. If all of the tests passed, then you'll want to make sure that you added the
  252. new test shown above to the appropriate folder and file name.
  253. Writing the code for your ticket
  254. ================================
  255. Next we'll be adding the ``make_toast()`` function.
  256. Navigate to the ``django/`` folder and open the ``shortcuts.py`` file. At the
  257. bottom, add::
  258. def make_toast():
  259. return 'toast'
  260. Now we need to make sure that the test we wrote earlier passes, so we can see
  261. whether the code we added is working correctly. Again, navigate to the Django
  262. ``tests/`` directory and run:
  263. .. console::
  264. $ ./runtests.py shortcuts
  265. Everything should pass. If it doesn't, make sure you correctly added the
  266. function to the correct file.
  267. Running Django's test suite for the second time
  268. ===============================================
  269. Once you've verified that your patch and your test are working correctly, it's
  270. a good idea to run the entire Django test suite just to verify that your change
  271. hasn't introduced any bugs into other areas of Django. While successfully
  272. passing the entire test suite doesn't guarantee your code is bug free, it does
  273. help identify many bugs and regressions that might otherwise go unnoticed.
  274. To run the entire Django test suite, ``cd`` into the Django ``tests/``
  275. directory and run:
  276. .. console::
  277. $ ./runtests.py
  278. Writing Documentation
  279. =====================
  280. This is a new feature, so it should be documented. Open the file
  281. ``docs/topics/http/shortcuts.txt`` and add the following at the end of the
  282. file::
  283. ``make_toast()``
  284. ================
  285. .. versionadded:: 2.2
  286. Returns ``'toast'``.
  287. Since this new feature will be in an upcoming release it is also added to the
  288. release notes for the next version of Django. Open the release notes for the
  289. latest version in ``docs/releases/``, which at time of writing is ``2.2.txt``.
  290. Add a note under the "Minor Features" header::
  291. :mod:`django.shortcuts`
  292. ~~~~~~~~~~~~~~~~~~~~~~~
  293. * The new :func:`django.shortcuts.make_toast` function returns ``'toast'``.
  294. For more information on writing documentation, including an explanation of what
  295. the ``versionadded`` bit is all about, see
  296. :doc:`/internals/contributing/writing-documentation`. That page also includes
  297. an explanation of how to build a copy of the documentation locally, so you can
  298. preview the HTML that will be generated.
  299. Previewing your changes
  300. =======================
  301. Now it's time to go through all the changes made in our patch. To stage all the
  302. changes ready for commit, run:
  303. .. console::
  304. $ git add --all
  305. Then display the differences between your current copy of Django (with your
  306. changes) and the revision that you initially checked out earlier in the
  307. tutorial with:
  308. .. console::
  309. $ git diff --cached
  310. Use the arrow keys to move up and down.
  311. .. code-block:: diff
  312. diff --git a/django/shortcuts.py b/django/shortcuts.py
  313. index 7ab1df0e9d..8dde9e28d9 100644
  314. --- a/django/shortcuts.py
  315. +++ b/django/shortcuts.py
  316. @@ -156,3 +156,7 @@ def resolve_url(to, *args, **kwargs):
  317. # Finally, fall back and assume it's a URL
  318. return to
  319. +
  320. +
  321. +def make_toast():
  322. + return 'toast'
  323. diff --git a/docs/releases/2.2.txt b/docs/releases/2.2.txt
  324. index 7d85d30c4a..81518187b3 100644
  325. --- a/docs/releases/2.2.txt
  326. +++ b/docs/releases/2.2.txt
  327. @@ -40,6 +40,11 @@ database constraints. Constraints are added to models using the
  328. Minor features
  329. --------------
  330. +:mod:`django.shortcuts`
  331. +~~~~~~~~~~~~~~~~~~~~~~~
  332. +
  333. +* The new :func:`django.shortcuts.make_toast` function returns ``'toast'``.
  334. +
  335. :mod:`django.contrib.admin`
  336. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  337. diff --git a/docs/topics/http/shortcuts.txt b/docs/topics/http/shortcuts.txt
  338. index 7b3a3a2c00..711bf6bb6d 100644
  339. --- a/docs/topics/http/shortcuts.txt
  340. +++ b/docs/topics/http/shortcuts.txt
  341. @@ -271,3 +271,12 @@ This example is equivalent to::
  342. my_objects = list(MyModel.objects.filter(published=True))
  343. if not my_objects:
  344. raise Http404("No MyModel matches the given query.")
  345. +
  346. +``make_toast()``
  347. +================
  348. +
  349. +.. function:: make_toast()
  350. +
  351. +.. versionadded:: 2.2
  352. +
  353. +Returns ``'toast'``.
  354. diff --git a/tests/shortcuts/test_make_toast.py b/tests/shortcuts/test_make_toast.py
  355. new file mode 100644
  356. index 0000000000..6f4c627b6e
  357. --- /dev/null
  358. +++ b/tests/shortcuts/test_make_toast.py
  359. @@ -0,0 +1,7 @@
  360. +from django.shortcuts import make_toast
  361. +from django.test import SimpleTestCase
  362. +
  363. +
  364. +class MakeToastTests(SimpleTestCase):
  365. + def test_make_toast(self):
  366. + self.assertEqual(make_toast(), 'toast')
  367. When you're done previewing the patch, hit the ``q`` key to return to the
  368. command line. If the patch's content looked okay, it's time to commit the
  369. changes.
  370. Committing the changes in the patch
  371. ===================================
  372. To commit the changes:
  373. .. console::
  374. $ git commit
  375. This opens up a text editor to type the commit message. Follow the :ref:`commit
  376. message guidelines <committing-guidelines>` and write a message like:
  377. .. code-block:: text
  378. Fixed #99999 -- Added a shortcut function to make toast.
  379. Pushing the commit and making a pull request
  380. ============================================
  381. After committing the patch, send it to your fork on GitHub (substitute
  382. "ticket_99999" with the name of your branch if it's different):
  383. .. console::
  384. $ git push origin ticket_99999
  385. You can create a pull request by visiting the `Django GitHub page
  386. <https://github.com/django/django/>`_. You'll see your branch under "Your
  387. recently pushed branches". Click "Compare & pull request" next to it.
  388. Please don't do it for this tutorial, but on the next page that displays a
  389. preview of the patch, you would click "Create pull request".
  390. Next steps
  391. ==========
  392. Congratulations, you've learned how to make a pull request to Django! Details
  393. of more advanced techniques you may need are in
  394. :doc:`/internals/contributing/writing-code/working-with-git`.
  395. Now you can put those skills to good use by helping to improve Django's
  396. codebase.
  397. More information for new contributors
  398. -------------------------------------
  399. Before you get too into writing patches for Django, there's a little more
  400. information on contributing that you should probably take a look at:
  401. * You should make sure to read Django's documentation on
  402. :doc:`claiming tickets and submitting patches
  403. </internals/contributing/writing-code/submitting-patches>`.
  404. It covers Trac etiquette, how to claim tickets for yourself, expected
  405. coding style for patches, and many other important details.
  406. * First time contributors should also read Django's :doc:`documentation
  407. for first time contributors</internals/contributing/new-contributors/>`.
  408. It has lots of good advice for those of us who are new to helping out
  409. with Django.
  410. * After those, if you're still hungry for more information about
  411. contributing, you can always browse through the rest of
  412. :doc:`Django's documentation on contributing</internals/contributing/index>`.
  413. It contains a ton of useful information and should be your first source
  414. for answering any questions you might have.
  415. Finding your first real ticket
  416. ------------------------------
  417. Once you've looked through some of that information, you'll be ready to go out
  418. and find a ticket of your own to write a patch for. Pay special attention to
  419. tickets with the "easy pickings" criterion. These tickets are often much
  420. simpler in nature and are great for first time contributors. Once you're
  421. familiar with contributing to Django, you can move on to writing patches for
  422. more difficult and complicated tickets.
  423. If you just want to get started already (and nobody would blame you!), try
  424. taking a look at the list of `easy tickets that need patches`__ and the
  425. `easy tickets that have patches which need improvement`__. If you're familiar
  426. with writing tests, you can also look at the list of
  427. `easy tickets that need tests`__. Just remember to follow the guidelines about
  428. claiming tickets that were mentioned in the link to Django's documentation on
  429. :doc:`claiming tickets and submitting patches
  430. </internals/contributing/writing-code/submitting-patches>`.
  431. __ https://code.djangoproject.com/query?status=new&status=reopened&has_patch=0&easy=1&col=id&col=summary&col=status&col=owner&col=type&col=milestone&order=priority
  432. __ https://code.djangoproject.com/query?status=new&status=reopened&needs_better_patch=1&easy=1&col=id&col=summary&col=status&col=owner&col=type&col=milestone&order=priority
  433. __ https://code.djangoproject.com/query?status=new&status=reopened&needs_tests=1&easy=1&col=id&col=summary&col=status&col=owner&col=type&col=milestone&order=priority
  434. What's next after creating a pull request?
  435. ------------------------------------------
  436. After a ticket has a patch, it needs to be reviewed by a second set of eyes.
  437. After submitting a pull request, update the ticket metadata by setting the
  438. flags on the ticket to say "has patch", "doesn't need tests", etc, so others
  439. can find it for review. Contributing doesn't necessarily always mean writing a
  440. patch from scratch. Reviewing existing patches is also a very helpful
  441. contribution. See :doc:`/internals/contributing/triaging-tickets` for details.