- oioioi.contests.current_contest.reverse(target, *args, **kwargs)¶
A modified URL reverser that takes into account the current contest and generates URLs that are appropriately prefixed. With it we substitute the original
The choice of prefixing the URL with a particular contest ID (or not prefixing at all) by the function is made as follows:
contest_idkwarg is given which is not None then the URL, if successfully reversed, is prefixed with it.
contest_idkwarg equal to None is given then the URL, if successfully reversed, will not be prefixed.
If the kwarg isn’t given but a contest is active when calling the function then that contest is used for the generated URL.
If the above fails or there is no active contest then no contest will be used.
Our reverser uses the special structure of each app’s urls.py file:
Urls pointing to views that require a contest are defined in the
contest_patternspattern list. Those only have a contest-prefixed version.
Urls pointing to views that require no contest being active are defined in the
noncontest_patternspattern list. Those only have a non contest-prefixed version.
Urls pointing to views that can run both with and without current contest are defined in the
urlpatternspattern list. Those have both versions.
These files are preprocessed to be used by the reverser. Urls defined in
oioioi.urlsare not preprocessed, so they only have a non-prefixed version, even though they could exist within a contest.
Note that there is no point defining patterns that receive a
contest_idkwarg. That particular kwarg is interpreted differently and will never be actually matched in the url pattern when reversing.
You need to take into account the behavior of reverse when defining your own custom urlconf (that means patterns lying outside an app’s urls.py file, e.g. for testing purposes), because it won’t be preprocessed. For that we created the