| | 1 | =================================== |
| | 2 | Writing your first patch for Django |
| | 3 | =================================== |
| | 4 | |
| | 5 | Introduction |
| | 6 | ============ |
| | 7 | |
| | 8 | Interested in giving back to the community a little? Maybe you've found a bug |
| | 9 | in Django that you'd like to see fixed, or maybe there's a small feature you |
| | 10 | want added. |
| | 11 | |
| | 12 | Contributing back to Django itself is the best way to see your own concerns |
| | 13 | addressed. This may seem daunting at first, but it's really pretty simple. |
| | 14 | We'll walk you through the entire process, so you can learn by example. |
| | 15 | |
| | 16 | Who's this tutorial for? |
| | 17 | ------------------------ |
| | 18 | |
| | 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 | |
| | 26 | Those of you who are unfamiliar with version control systems and Trac will find |
| | 27 | that this tutorial and its links include just enough information to get started. |
| | 28 | However, you'll probably want to read some more about these different tools if |
| | 29 | you plan on contributing to Django regularly. |
| | 30 | |
| | 31 | For the most part though, this tutorial tries to explain as much as possible, |
| | 32 | so that it can be of use to the widest audience. |
| | 33 | |
| | 34 | .. admonition:: Where to get help: |
| | 35 | |
| | 36 | If you're having trouble going through this tutorial, please post a message |
| | 37 | to `django-developers`__ or drop by `#django-dev on irc.freenode.net`__ to chat |
| | 38 | with other Django users who might be able to help. |
| | 39 | |
| | 40 | __ http://diveintopython.net/toc/index.html |
| | 41 | __ http://groups.google.com/group/django-developers |
| | 42 | __ irc://irc.freenode.net/django-dev |
| | 43 | |
| | 44 | What does this tutorial cover? |
| | 45 | ------------------------------ |
| | 46 | |
| | 47 | We'll be walking you through contributing a patch to Django for the first time. |
| | 48 | By the end of this tutorial, you should have a basic understanding of both the |
| | 49 | tools and the processes involved. Specifically, we'll be covering the following: |
| | 50 | |
| | 51 | * Installing Git. |
| | 52 | * How to download a development copy of Django. |
| | 53 | * Running Django's test suite. |
| | 54 | * Writing a test for your patch. |
| | 55 | * Writing the code for your patch. |
| | 56 | * Testing your patch. |
| | 57 | * Generating a patch file for your changes. |
| | 58 | * Where to look for more information. |
| | 59 | |
| | 60 | Once you're done with the tutorial, you can look through the rest of |
| | 61 | :doc:`Django's documentation on contributing</internals/contributing/index>`. |
| | 62 | It contains lots of great information and is a must read for anyone who'd like |
| | 63 | to become a regular contributor to Django. If you've got questions, it's |
| | 64 | probably got the answers. |
| | 65 | |
| | 66 | Installing Git |
| | 67 | ============== |
| | 68 | |
| | 69 | For this tutorial, you'll need Git installed to download the current |
| | 70 | development version of Django and to generate patch files for the changes you |
| | 71 | make. |
| | 72 | |
| | 73 | To check whether or not you have Git installed, enter ``git`` into the command |
| | 74 | line. If you get messages saying that this command could be found, you'll have |
| | 75 | to download and install it, see `Git's download page`__. |
| | 76 | |
| | 77 | If you're not that familiar with Git, you can always find out more about its |
| | 78 | commands (once it's installed) by typing ``git help`` into the command line. |
| | 79 | |
| | 80 | __ http://git-scm.com/download |
| | 81 | |
| | 82 | Getting a copy of Django's development version |
| | 83 | ============================================== |
| | 84 | |
| | 85 | The first step to contributing to Django is to get a copy of the source code. |
| | 86 | From the command line, use the ``cd`` command to navigate to the directory |
| | 87 | where you'll want your local copy of Django to live. |
| | 88 | |
| | 89 | Download the Django source code repository using the following command:: |
| | 90 | |
| | 91 | git clone https://github.com/django/django.git |
| | 92 | |
| | 93 | .. note:: |
| | 94 | |
| | 95 | For users who wish to use `virtualenv`__, you can use:: |
| | 96 | |
| | 97 | pip install -e /path/to/your/local/clone/django/ |
| | 98 | |
| | 99 | to link your cloned checkout into a virtual environment. This is a great |
| | 100 | option to isolate your development copy of Django from the rest of your |
| | 101 | system and avoids potential package conflicts. |
| | 102 | |
| | 103 | __ http://www.virtualenv.org |
| | 104 | |
| | 105 | Rolling back to a previous revision of Django |
| | 106 | ============================================= |
| | 107 | |
| | 108 | For this tutorial, we'll be using `ticket #17549`__ as a case study, so we'll |
| | 109 | rewind Django's version history in git to before that ticket's patch was |
| | 110 | applied. This will allow us to go through all of the steps involved in writing |
| | 111 | that patch from scratch, including running Django's test suite. |
| | 112 | |
| | 113 | **Keep in mind that while we'll be using an older revision of Django's trunk |
| | 114 | for the purposes of the tutorial below, you should always use the current |
| | 115 | development revision of Django when working on your own patch for a ticket!** |
| | 116 | |
| | 117 | .. note:: |
| | 118 | |
| | 119 | The patch for this ticket was written by Ulrich Petri, and it was applied |
| | 120 | to Django as `commit ac2052ebc84c45709ab5f0f25e685bf656ce79bc`__. |
| | 121 | Consequently, we'll be using the revision of Django just prior to that, |
| | 122 | `commit 39f5bc7fc3a4bb43ed8a1358b17fe0521a1a63ac`__. |
| | 123 | |
| | 124 | __ https://code.djangoproject.com/ticket/17549 |
| | 125 | __ https://github.com/django/django/commit/ac2052ebc84c45709ab5f0f25e685bf656ce79bc |
| | 126 | __ https://github.com/django/django/commit/39f5bc7fc3a4bb43ed8a1358b17fe0521a1a63ac |
| | 127 | |
| | 128 | Navigate into Django's root directory (that's the one that contains ``django``, |
| | 129 | ``docs``, ``tests``, ``AUTHORS``, etc.). You can then check out the older |
| | 130 | revision of Django that we'll be using in the tutorial below:: |
| | 131 | |
| | 132 | git checkout 39f5bc7fc3a4bb43ed8a1358b17fe0521a1a63ac |
| | 133 | |
| | 134 | Running Django's test suite for the first time |
| | 135 | ============================================== |
| | 136 | |
| | 137 | When contributing to Django it's very important that your code changes don't |
| | 138 | introduce bugs into other areas of Django. One way to check that Django still |
| | 139 | works after you make your changes is by running Django's test suite. If all |
| | 140 | the tests still pass, then you can be reasonably sure that your changes |
| | 141 | haven't completely broken Django. If you've never run Django's test suite |
| | 142 | before, it's a good idea to run it once beforehand just to get familiar with |
| | 143 | what its output is supposed to look like. |
| | 144 | |
| | 145 | We can run the test suite by simply ``cd``-ing into the Django ``tests/`` |
| | 146 | directory and, if you're using GNU/Linux, Mac OS X or some other flavor of |
| | 147 | Unix, run:: |
| | 148 | |
| | 149 | PYTHONPATH=.. python runtests.py --settings=test_sqlite |
| | 150 | |
| | 151 | If you're on Windows, the above should work provided that you are using |
| | 152 | "Git Bash" provided by the default Git install. GitHub has a `nice tutorial`__. |
| | 153 | |
| | 154 | __ https://help.github.com/articles/set-up-git#platform-windows |
| | 155 | |
| | 156 | .. note:: |
| | 157 | |
| | 158 | If you're using ``virtualenv``, you can omit ``PYTHONPATH=..`` when running |
| | 159 | the tests. This instructs Python to look for Django in the parent directory |
| | 160 | of ``tests``. ``virtualenv`` puts your copy of Django on the ``PYTHONPATH`` |
| | 161 | automatically. |
| | 162 | |
| | 163 | Now sit back and relax. Django's entire test suite has over 4800 different |
| | 164 | tests, so it can take anywhere from 5 to 15 minutes to run, depending on the |
| | 165 | speed of your computer. |
| | 166 | |
| | 167 | While Django's test suite is running, you'll see a stream of characters |
| | 168 | representing the status of each test as it's run. ``E`` indicates that an error |
| | 169 | was raised during a test, and ``F`` indicates that a test's assertions failed. |
| | 170 | Both of these are considered to be test failures. Meanwhile, ``x`` and ``s`` |
| | 171 | indicated expected failures and skipped tests, respectively. Dots indicate |
| | 172 | passing tests. |
| | 173 | |
| | 174 | Skipped tests are typically due to missing external libraries required to run |
| | 175 | the test; see :ref:`running-unit-tests-dependencies` for a list of dependencies |
| | 176 | and be sure to install any for tests related to the changes you are making (we |
| | 177 | won't need any for this tutorial). |
| | 178 | |
| | 179 | Once the tests complete, you should be greeted with a message informing you |
| | 180 | whether the test suite passed or failed. Since you haven't yet made any changes |
| | 181 | to Django's code, the entire test suite **should** pass. If you get failures or |
| | 182 | errors make sure you've followed all of the previous steps properly. See |
| | 183 | :ref:`running-unit-tests` for more information. |
| | 184 | |
| | 185 | Note that the latest Django trunk may not always be stable. When developing |
| | 186 | against trunk, you can check `Django's continuous integration builds`__ to |
| | 187 | determine if the failures are specific to your machine or if they are also |
| | 188 | present in Django's official builds. |
| | 189 | |
| | 190 | __ http://ci.djangoproject.com/ |
| | 191 | |
| | 192 | .. note:: |
| | 193 | |
| | 194 | For this tutorial and the ticket we're working on, testing against SQLite |
| | 195 | is sufficient, however, it's possible (and sometimes necessary) to |
| | 196 | :ref:`run the tests using a different database |
| | 197 | <running-unit-tests-settings>`. |
| | 198 | |
| | 199 | Writing some tests for your ticket |
| | 200 | ================================== |
| | 201 | |
| | 202 | In most cases, for a patch to be accepted into Django it has to include tests. |
| | 203 | For bug fix patches, this means writing a regression test to ensure that the |
| | 204 | bug is never reintroduced into Django later on. A regression test should be |
| | 205 | written in such a way that it will fail while the bug still exists and pass |
| | 206 | once the bug has been fixed. For patches containing new features, you'll need |
| | 207 | to include tests which ensure that the new features are working correctly. |
| | 208 | They too should fail when the new feature is not present, and then pass once it |
| | 209 | has been implemented. |
| | 210 | |
| | 211 | A good way to do this is to write your new tests first, before making any |
| | 212 | changes to the code. This style of development is called |
| | 213 | `test-driven development`__ and can be applied to both entire projects and |
| | 214 | single patches. After writing your tests, you then run them to make sure that |
| | 215 | they do indeed fail (since you haven't fixed that bug or added that feature |
| | 216 | yet). If your new tests don't fail, you'll need to fix them so that they do. |
| | 217 | After all, a regression test that passes regardless of whether a bug is present |
| | 218 | is not very helpful at preventing that bug from reoccurring down the road. |
| | 219 | |
| | 220 | Now for our hands-on example. |
| | 221 | |
| | 222 | __ http://en.wikipedia.org/wiki/Test-driven_development |
| | 223 | |
| | 224 | Writing some tests for ticket #17549 |
| | 225 | ------------------------------------ |
| | 226 | |
| | 227 | `Ticket #17549`__ describes the following, small feature addition: |
| | 228 | |
| | 229 | It's useful for URLField to give you a way to open the URL; otherwise you |
| | 230 | might as well use a CharField. |
| | 231 | |
| | 232 | In order to resolve this ticket, we'll add a ``render`` method to the |
| | 233 | ``AdminURLFieldWidget`` in order to display a clickable link above the input |
| | 234 | widget. Before we make those changes though, we're going to write a couple |
| | 235 | tests to verify that our modification functions correctly and continues to |
| | 236 | function correctly in the future. |
| | 237 | |
| | 238 | Navigate to Django's ``tests/regressiontests/admin_widgets/`` folder and |
| | 239 | open the ``tests.py`` file. Add the following code on line 269 right before the |
| | 240 | ``AdminFileWidgetTest`` class:: |
| | 241 | |
| | 242 | class AdminURLWidgetTest(DjangoTestCase): |
| | 243 | def test_render(self): |
| | 244 | w = widgets.AdminURLFieldWidget() |
| | 245 | self.assertHTMLEqual( |
| | 246 | conditional_escape(w.render('test', '')), |
| | 247 | '<input class="vURLField" name="test" type="text" />' |
| | 248 | ) |
| | 249 | self.assertHTMLEqual( |
| | 250 | conditional_escape(w.render('test', 'http://example.com')), |
| | 251 | '<p class="url">Currently:<a href="http://example.com">http://example.com</a><br />Change:<input class="vURLField" name="test" type="text" value="http://example.com" /></p>' |
| | 252 | ) |
| | 253 | |
| | 254 | def test_render_idn(self): |
| | 255 | w = widgets.AdminURLFieldWidget() |
| | 256 | self.assertHTMLEqual( |
| | 257 | conditional_escape(w.render('test', 'http://example-äüö.com')), |
| | 258 | '<p class="url">Currently:<a href="http://xn--example--7za4pnc.com">http://example-äüö.com</a><br />Change:<input class="vURLField" name="test" type="text" value="http://example-äüö.com" /></p>' |
| | 259 | ) |
| | 260 | |
| | 261 | def test_render_quoting(self): |
| | 262 | w = widgets.AdminURLFieldWidget() |
| | 263 | self.assertHTMLEqual( |
| | 264 | conditional_escape(w.render('test', 'http://example.com/<sometag>some text</sometag>')), |
| | 265 | '<p class="url">Currently:<a href="http://example.com/%3Csometag%3Esome%20text%3C/sometag%3E">http://example.com/<sometag>some text</sometag></a><br />Change:<input class="vURLField" name="test" type="text" value="http://example.com/<sometag>some text</sometag>" /></p>' |
| | 266 | ) |
| | 267 | self.assertHTMLEqual( |
| | 268 | conditional_escape(w.render('test', 'http://example-äüö.com/<sometag>some text</sometag>')), |
| | 269 | '<p class="url">Currently:<a href="http://xn--example--7za4pnc.com/%3Csometag%3Esome%20text%3C/sometag%3E">http://example-äüö.com/<sometag>some text</sometag></a><br />Change:<input class="vURLField" name="test" type="text" value="http://example-äüö.com/<sometag>some text</sometag>" /></p>' |
| | 270 | ) |
| | 271 | |
| | 272 | The new tests check to see that the ``render`` method we'll be adding works |
| | 273 | correctly in a couple different situations. |
| | 274 | |
| | 275 | .. admonition:: But this testing thing looks kinda hard... |
| | 276 | |
| | 277 | If you've never had to deal with tests before, they can look a little hard |
| | 278 | to write at first glance. Fortunately, testing is a *very* big subject in |
| | 279 | computer programming, so there's lots of information out there: |
| | 280 | |
| | 281 | * A good first look at writing tests for Django can be found in the |
| | 282 | documentation on :doc:`Testing Django applications</topics/testing/>`. |
| | 283 | * Dive Into Python (a free online book for beginning Python developers) |
| | 284 | includes a great `introduction to Unit Testing`__. |
| | 285 | * After reading those, if you want something a little meatier to sink |
| | 286 | your teeth into, there's always the `Python unittest documentation`__. |
| | 287 | |
| | 288 | __ https://code.djangoproject.com/ticket/17549 |
| | 289 | __ http://diveintopython.net/unit_testing/index.html |
| | 290 | __ http://docs.python.org/library/unittest.html |
| | 291 | |
| | 292 | Running your new test |
| | 293 | --------------------- |
| | 294 | |
| | 295 | Remember that we haven't actually made any modifications to |
| | 296 | ``AdminURLFieldWidget`` yet, so our tests are going to fail. Let's run all the |
| | 297 | tests in the ``model_forms_regress`` folder to make sure that's really what |
| | 298 | happens. From the command line, ``cd`` into the Django ``tests/`` directory |
| | 299 | and run:: |
| | 300 | |
| | 301 | PYTHONPATH=.. python runtests.py --settings=test_sqlite admin_widgets |
| | 302 | |
| | 303 | If the tests ran correctly, you should see three failures corresponding to each |
| | 304 | of the test methods we added. If all of the tests passed, then you'll want to |
| | 305 | make sure that you added the new test shown above to the appropriate folder and |
| | 306 | class. |
| | 307 | |
| | 308 | Writing the code for your ticket |
| | 309 | ================================ |
| | 310 | |
| | 311 | Next we'll be adding the functionality described in `ticket #17549`__ to Django. |
| | 312 | |
| | 313 | Writing the code for ticket #17549 |
| | 314 | ---------------------------------- |
| | 315 | |
| | 316 | Navigate to the ``django/django/contrib/admin/`` folder and open the |
| | 317 | ``widgets.py`` file. Find the ``AdminURLFieldWidget`` class on line 302 and add |
| | 318 | the following ``render`` method after the existing ``__init__`` method:: |
| | 319 | |
| | 320 | def render(self, name, value, attrs=None): |
| | 321 | html = super(AdminURLFieldWidget, self).render(name, value, attrs) |
| | 322 | if value: |
| | 323 | value = force_text(self._format_value(value)) |
| | 324 | final_attrs = {'href': mark_safe(smart_urlquote(value))} |
| | 325 | html = format_html( |
| | 326 | '<p class="url">{0} <a {1}>{2}</a><br />{3} {4}</p>', |
| | 327 | _('Currently:'), flatatt(final_attrs), value, |
| | 328 | _('Change:'), html |
| | 329 | ) |
| | 330 | return html |
| | 331 | |
| | 332 | Verifying your test now passes |
| | 333 | ------------------------------ |
| | 334 | |
| | 335 | Once you're done modifying Django, we need to make sure that the tests we wrote |
| | 336 | earlier pass, so we can see whether the code we wrote above is working |
| | 337 | correctly. To run the tests in the ``admin_widgets`` folder, ``cd`` into the |
| | 338 | Django ``tests/`` directory and run:: |
| | 339 | |
| | 340 | PYTHONPATH=.. python runtests.py --settings=test_sqlite admin_widgets |
| | 341 | |
| | 342 | Oops, good thing we wrote those tests! You should still see 3 failures with |
| | 343 | the following exception:: |
| | 344 | |
| | 345 | NameError: global name 'smart_urlquote' is not defined |
| | 346 | |
| | 347 | We forgot to add the import for that method. Go ahead and add the |
| | 348 | ``smart_urlquote`` import at the end of line 13 of |
| | 349 | ``django/contrib/admin/widgets.py`` so it looks as follows:: |
| | 350 | |
| | 351 | from django.utils.html import escape, format_html, format_html_join, smart_urlquote |
| | 352 | |
| | 353 | Re-run the tests and everything should pass. If it doesn't, make sure you |
| | 354 | correctly modified the ``AdminURLFieldWidget`` class as shown above and |
| | 355 | copied the new tests correctly. |
| | 356 | |
| | 357 | __ https://code.djangoproject.com/ticket/17549 |
| | 358 | |
| | 359 | Running Django's test suite for the second time |
| | 360 | =============================================== |
| | 361 | |
| | 362 | Once you've verified that your patch and your test are working correctly, it's |
| | 363 | a good idea to run the entire Django test suite just to verify that your change |
| | 364 | hasn't introduced any bugs into other areas of Django. While successfully |
| | 365 | passing the entire test suite doesn't guarantee your code is bug free, it does |
| | 366 | help identify many bugs and regressions that might otherwise go unnoticed. |
| | 367 | |
| | 368 | To run the entire Django test suite, ``cd`` into the Django ``tests/`` |
| | 369 | directory and run:: |
| | 370 | |
| | 371 | PYTHONPATH=.. python runtests.py --settings=test_sqlite |
| | 372 | |
| | 373 | As long as you don't see any failures, you're good to go. Note that this fix |
| | 374 | also made a `small CSS change`__ to format the new widget. You can make the |
| | 375 | change if you'd like, but we'll skip it for now in the interest of brevity. |
| | 376 | |
| | 377 | __ https://github.com/django/django/commit/ac2052ebc84c45709ab5f0f25e685bf656ce79bc#diff-0 |
| | 378 | |
| | 379 | Writing Documentation |
| | 380 | ===================== |
| | 381 | |
| | 382 | This is a new feature, so it should be documented. Add the following on line |
| | 383 | 925 of ``django/docs/ref/models/fields.txt`` beneath the existing docs for |
| | 384 | ``URLField``:: |
| | 385 | |
| | 386 | .. versionadded:: 1.5 |
| | 387 | |
| | 388 | The current value of the field will be displayed as a clickable link above the |
| | 389 | input widget. |
| | 390 | |
| | 391 | For more information on writing documentation, including an explanation of what |
| | 392 | the ``versionadded`` bit is all about, see |
| | 393 | :doc:`/internals/contributing/writing-documentation`. That page also includes |
| | 394 | an explanation of how to build a copy of the documentation locally, so you can |
| | 395 | preview the HTML that will be generated. |
| | 396 | |
| | 397 | Generating a patch for your changes |
| | 398 | =================================== |
| | 399 | |
| | 400 | Now it's time to generate a patch file that can be uploaded to Trac or applied |
| | 401 | to another copy of Django. To get a look at the content of your patch, run the |
| | 402 | following command:: |
| | 403 | |
| | 404 | git diff |
| | 405 | |
| | 406 | This will display the differences between your current copy of Django (with |
| | 407 | your changes) and the revision that you initially checked out earlier in the |
| | 408 | tutorial. |
| | 409 | |
| | 410 | Once you're done looking at the patch, hit the ``q`` key to exit back to the |
| | 411 | command line. If the patch's content looked okay, you can run the following |
| | 412 | command to save the patch file to your current working directory:: |
| | 413 | |
| | 414 | git diff > 17549.diff |
| | 415 | |
| | 416 | You should now have a file in the root Django directory called ``17549.diff``. |
| | 417 | This patch file contains all your changes and should look this: |
| | 418 | |
| | 419 | .. code-block:: diff |
| | 420 | |
| | 421 | diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py |
| | 422 | index 1e0bc2d..9e43a10 100644 |
| | 423 | --- a/django/contrib/admin/widgets.py |
| | 424 | +++ b/django/contrib/admin/widgets.py |
| | 425 | @@ -10,7 +10,7 @@ from django.contrib.admin.templatetags.admin_static import static |
| | 426 | from django.core.urlresolvers import reverse |
| | 427 | from django.forms.widgets import RadioFieldRenderer |
| | 428 | from django.forms.util import flatatt |
| | 429 | -from django.utils.html import escape, format_html, format_html_join |
| | 430 | +from django.utils.html import escape, format_html, format_html_join, smart_urlquote |
| | 431 | from django.utils.text import Truncator |
| | 432 | from django.utils.translation import ugettext as _ |
| | 433 | from django.utils.safestring import mark_safe |
| | 434 | @@ -306,6 +306,18 @@ class AdminURLFieldWidget(forms.TextInput): |
| | 435 | final_attrs.update(attrs) |
| | 436 | super(AdminURLFieldWidget, self).__init__(attrs=final_attrs) |
| | 437 | |
| | 438 | + def render(self, name, value, attrs=None): |
| | 439 | + html = super(AdminURLFieldWidget, self).render(name, value, attrs) |
| | 440 | + if value: |
| | 441 | + value = force_text(self._format_value(value)) |
| | 442 | + final_attrs = {'href': mark_safe(smart_urlquote(value))} |
| | 443 | + html = format_html( |
| | 444 | + '<p class="url">{0} <a {1}>{2}</a><br />{3} {4}</p>', |
| | 445 | + _('Currently:'), flatatt(final_attrs), value, |
| | 446 | + _('Change:'), html |
| | 447 | + ) |
| | 448 | + return html |
| | 449 | + |
| | 450 | class AdminIntegerFieldWidget(forms.TextInput): |
| | 451 | class_name = 'vIntegerField' |
| | 452 | |
| | 453 | diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt |
| | 454 | index 809d56e..d44f85f 100644 |
| | 455 | --- a/docs/ref/models/fields.txt |
| | 456 | +++ b/docs/ref/models/fields.txt |
| | 457 | @@ -922,6 +922,10 @@ Like all :class:`CharField` subclasses, :class:`URLField` takes the optional |
| | 458 | :attr:`~CharField.max_length`argument. If you don't specify |
| | 459 | :attr:`~CharField.max_length`, a default of 200 is used. |
| | 460 | |
| | 461 | +.. versionadded:: 1.5 |
| | 462 | + |
| | 463 | +The current value of the field will be displayed as a clickable link above the |
| | 464 | +input widget. |
| | 465 | |
| | 466 | Relationship fields |
| | 467 | =================== |
| | 468 | diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py |
| | 469 | index 4b11543..94acc6d 100644 |
| | 470 | --- a/tests/regressiontests/admin_widgets/tests.py |
| | 471 | +++ b/tests/regressiontests/admin_widgets/tests.py |
| | 472 | @@ -265,6 +265,35 @@ class AdminSplitDateTimeWidgetTest(DjangoTestCase): |
| | 473 | '<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>', |
| | 474 | ) |
| | 475 | |
| | 476 | +class AdminURLWidgetTest(DjangoTestCase): |
| | 477 | + def test_render(self): |
| | 478 | + w = widgets.AdminURLFieldWidget() |
| | 479 | + self.assertHTMLEqual( |
| | 480 | + conditional_escape(w.render('test', '')), |
| | 481 | + '<input class="vURLField" name="test" type="text" />' |
| | 482 | + ) |
| | 483 | + self.assertHTMLEqual( |
| | 484 | + conditional_escape(w.render('test', 'http://example.com')), |
| | 485 | + '<p class="url">Currently:<a href="http://example.com">http://example.com</a><br />Change:<input class="vURLField" name="test" type="text" value="http://example.com" /></p>' |
| | 486 | + ) |
| | 487 | + |
| | 488 | + def test_render_idn(self): |
| | 489 | + w = widgets.AdminURLFieldWidget() |
| | 490 | + self.assertHTMLEqual( |
| | 491 | + conditional_escape(w.render('test', 'http://example-äüö.com')), |
| | 492 | + '<p class="url">Currently:<a href="http://xn--example--7za4pnc.com">http://example-äüö.com</a><br />Change:<input class="vURLField" name="test" type="text" value="http://example-äüö.com" /></p>' |
| | 493 | + ) |
| | 494 | + |
| | 495 | + def test_render_quoting(self): |
| | 496 | + w = widgets.AdminURLFieldWidget() |
| | 497 | + self.assertHTMLEqual( |
| | 498 | + conditional_escape(w.render('test', 'http://example.com/<sometag>some text</sometag>')), |
| | 499 | + '<p class="url">Currently:<a href="http://example.com/%3Csometag%3Esome%20text%3C/sometag%3E">http://example.com/<sometag>some text</sometag></a><br />Change:<input class="vURLField" name="test" type="text" value="http://example.com/<sometag>some text</sometag>" /></p>' |
| | 500 | + ) |
| | 501 | + self.assertHTMLEqual( |
| | 502 | + conditional_escape(w.render('test', 'http://example-äüö.com/<sometag>some text</sometag>')), |
| | 503 | + '<p class="url">Currently:<a href="http://xn--example--7za4pnc.com/%3Csometag%3Esome%20text%3C/sometag%3E">http://example-äüö.com/<sometag>some text</sometag></a><br />Change:<input class="vURLField" name="test" type="text" value="http://example-äüö.com/<sometag>some text</sometag>" /></p>' |
| | 504 | + ) |
| | 505 | |
| | 506 | class AdminFileWidgetTest(DjangoTestCase): |
| | 507 | def test_render(self): |
| | 508 | |
| | 509 | So what do I do next? |
| | 510 | ===================== |
| | 511 | |
| | 512 | Congratulations, you've generated your very first Django patch! Now that you've |
| | 513 | got that under your belt, you can put those skills to good use by helping to |
| | 514 | improve Django's codebase. Generating patches and attaching them to Trac |
| | 515 | tickets is useful, however, since we are using git - adopting a more :doc:`git |
| | 516 | oriented workflow </internals/contributing/writing-code/working-with-git>` is |
| | 517 | recommended. |
| | 518 | |
| | 519 | Since we never committed our changes locally, perform the following to get your |
| | 520 | git branch back to a good starting point:: |
| | 521 | |
| | 522 | git reset --hard HEAD |
| | 523 | git checkout master |
| | 524 | |
| | 525 | More information for new contributors |
| | 526 | ------------------------------------- |
| | 527 | |
| | 528 | Before you get too into writing patches for Django, there's a little more |
| | 529 | information on contributing that you should probably take a look at: |
| | 530 | |
| | 531 | * You should make sure to read Django's documentation on |
| | 532 | :doc:`claiming tickets and submitting patches |
| | 533 | </internals/contributing/writing-code/submitting-patches>`. |
| | 534 | It covers Trac etiquette, how to claim tickets for yourself, expected |
| | 535 | coding style for patches, and many other important details. |
| | 536 | * First time contributors should also read Django's :doc:`documentation |
| | 537 | for first time contributors</internals/contributing/new-contributors/>`. |
| | 538 | It has lots of good advice for those of us who are new to helping out |
| | 539 | with Django. |
| | 540 | * After those, if you're still hungry for more information about |
| | 541 | contributing, you can always browse through the rest of |
| | 542 | :doc:`Django's documentation on contributing</internals/contributing/index>`. |
| | 543 | It contains a ton of useful information and should be your first source |
| | 544 | for answering any questions you might have. |
| | 545 | |
| | 546 | Finding your first real ticket |
| | 547 | ------------------------------ |
| | 548 | |
| | 549 | Once you've looked through some of that information, you'll be ready to go out |
| | 550 | and find a ticket of your own to write a patch for. Pay special attention to |
| | 551 | tickets with the "easy pickings" criterion. These tickets are often much |
| | 552 | simpler in nature and are great for first time contributors. Once you're |
| | 553 | familiar with contributing to Django, you can move on to writing patches for |
| | 554 | more difficult and complicated tickets. |
| | 555 | |
| | 556 | If you just want to get started already (and nobody would blame you!), try |
| | 557 | taking a look at the list of `easy tickets that need patches`__ and the |
| | 558 | `easy tickets that have patches which need improvement`__. If you're familiar |
| | 559 | with writing tests, you can also look at the list of |
| | 560 | `easy tickets that need tests`__. Just remember to follow the guidelines about |
| | 561 | claiming tickets that were mentioned in the link to Django's documentation on |
| | 562 | :doc:`claiming tickets and submitting patches |
| | 563 | </internals/contributing/writing-code/submitting-patches>`. |
| | 564 | |
| | 565 | __ 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 |
| | 566 | __ 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 |
| | 567 | __ 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 |
| | 568 | |
| | 569 | What's next? |
| | 570 | ------------ |
| | 571 | |
| | 572 | After a ticket has a patch, it needs to be reviewed by a second set of eyes. |
| | 573 | After uploading a patch or submitting a pull request, be sure to update the |
| | 574 | ticket metadata by setting the flags on the ticket to say "has patch", |
| | 575 | "doesn't need tests", etc, so others can find it for review. Contributing |
| | 576 | doesn't necessarily always mean writing a patch from scratch. Reviewing |
| | 577 | existing patches is also a very helpful contribution. See |
| | 578 | :doc:`/internals/contributing/triaging-tickets` for details. |