contributing.txt 24 KB

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