Loading docs/howto/custom-management-commands.txt +4 −2 Original line number Diff line number Diff line Loading @@ -106,11 +106,13 @@ options can be added in the :meth:`~BaseCommand.add_arguments` method like this: parser.add_argument('poll_id', nargs='+', type=int) # Named (optional) arguments parser.add_argument('--delete', parser.add_argument( '--delete', action='store_true', dest='delete', default=False, help='Delete poll instead of closing it') help='Delete poll instead of closing it', ) def handle(self, *args, **options): # ... Loading docs/howto/error-reporting.txt +6 −4 Original line number Diff line number Diff line Loading @@ -202,10 +202,12 @@ filtered out of error reports in a production environment (that is, where @sensitive_post_parameters('pass_word', 'credit_card_number') def record_user_profile(request): UserProfile.create(user=request.user, UserProfile.create( user=request.user, password=request.POST['pass_word'], credit_card=request.POST['credit_card_number'], name=request.POST['name']) name=request.POST['name'], ) ... In the above example, the values for the ``pass_word`` and Loading docs/howto/static-files/deployment.txt +3 −3 Original line number Diff line number Diff line Loading @@ -106,7 +106,7 @@ Here's how this might look in a fabfile:: project.rsync_project( remote_dir=env.remote_static_root, local_dir=env.local_static_root, delete = True delete=True, ) .. _staticfiles-from-cdn: Loading docs/intro/tutorial05.txt +9 −15 Original line number Diff line number Diff line Loading @@ -462,8 +462,7 @@ class: in the past, positive for questions that have yet to be published). """ time = timezone.now() + datetime.timedelta(days=days) return Question.objects.create(question_text=question_text, pub_date=time) return Question.objects.create(question_text=question_text, pub_date=time) class QuestionViewTests(TestCase): Loading Loading @@ -495,8 +494,7 @@ class: """ create_question(question_text="Future question.", days=30) response = self.client.get(reverse('polls:index')) self.assertContains(response, "No polls are available.", status_code=200) self.assertContains(response, "No polls are available.") self.assertQuerysetEqual(response.context['latest_question_list'], []) def test_index_view_with_future_question_and_past_question(self): Loading Loading @@ -580,10 +578,9 @@ in the future is not: The detail view of a question with a pub_date in the future should return a 404 not found. """ future_question = create_question(question_text='Future question.', days=5) response = self.client.get(reverse('polls:detail', args=(future_question.id,))) future_question = create_question(question_text='Future question.', days=5) url = reverse('polls:detail', args=(future_question.id,)) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_detail_view_with_a_past_question(self): Loading @@ -591,13 +588,10 @@ in the future is not: The detail view of a question with a pub_date in the past should display the question's text. """ past_question = create_question(question_text='Past Question.', days=-5) response = self.client.get(reverse('polls:detail', args=(past_question.id,))) self.assertContains(response, past_question.question_text, status_code=200) past_question = create_question(question_text='Past Question.', days=-5) url = reverse('polls:detail', args=(past_question.id,)) response = self.client.get(url) self.assertContains(response, past_question.question_text) Ideas for more tests -------------------- Loading docs/ref/contrib/admin/index.txt +18 −15 Original line number Diff line number Diff line Loading @@ -615,10 +615,12 @@ subclass:: color_code = models.CharField(max_length=6) def colored_name(self): return format_html('<span style="color: #{};">{} {}</span>', return format_html( '<span style="color: #{};">{} {}</span>', self.color_code, self.first_name, self.last_name) self.last_name, ) class PersonAdmin(admin.ModelAdmin): list_display = ('first_name', 'last_name', 'colored_name') Loading Loading @@ -700,9 +702,11 @@ subclass:: color_code = models.CharField(max_length=6) def colored_first_name(self): return format_html('<span style="color: #{};">{}</span>', return format_html( '<span style="color: #{};">{}</span>', self.color_code, self.first_name) self.first_name, ) colored_first_name.admin_order_field = 'first_name' Loading Loading @@ -906,13 +910,11 @@ subclass:: def lookups(self, request, model_admin): if request.user.is_superuser: return super(AuthDecadeBornListFilter, self).lookups(request, model_admin) return super(AuthDecadeBornListFilter, self).lookups(request, model_admin) def queryset(self, request, queryset): if request.user.is_superuser: return super(AuthDecadeBornListFilter, self).queryset(request, queryset) return super(AuthDecadeBornListFilter, self).queryset(request, queryset) Also as a convenience, the ``ModelAdmin`` object is passed to the ``lookups`` method, for example if you want to base the Loading Loading @@ -1268,8 +1270,8 @@ subclass:: class PersonAdmin(admin.ModelAdmin): def view_on_site(self, obj): return 'https://example.com' + reverse('person-detail', kwargs={'slug': obj.slug}) url = reverse('person-detail', kwargs={'slug': obj.slug}) return 'https://example.com' + url Custom template options ~~~~~~~~~~~~~~~~~~~~~~~ Loading Loading @@ -1875,8 +1877,9 @@ provided some extra mapping data that would not otherwise be available:: def change_view(self, request, object_id, form_url='', extra_context=None): extra_context = extra_context or {} extra_context['osm_data'] = self.get_osm_info() return super(MyModelAdmin, self).change_view(request, object_id, form_url, extra_context=extra_context) return super(MyModelAdmin, self).change_view( request, object_id, form_url, extra_context=extra_context, ) These views return :class:`~django.template.response.TemplateResponse` instances which allow you to easily customize the response data before Loading Loading
docs/howto/custom-management-commands.txt +4 −2 Original line number Diff line number Diff line Loading @@ -106,11 +106,13 @@ options can be added in the :meth:`~BaseCommand.add_arguments` method like this: parser.add_argument('poll_id', nargs='+', type=int) # Named (optional) arguments parser.add_argument('--delete', parser.add_argument( '--delete', action='store_true', dest='delete', default=False, help='Delete poll instead of closing it') help='Delete poll instead of closing it', ) def handle(self, *args, **options): # ... Loading
docs/howto/error-reporting.txt +6 −4 Original line number Diff line number Diff line Loading @@ -202,10 +202,12 @@ filtered out of error reports in a production environment (that is, where @sensitive_post_parameters('pass_word', 'credit_card_number') def record_user_profile(request): UserProfile.create(user=request.user, UserProfile.create( user=request.user, password=request.POST['pass_word'], credit_card=request.POST['credit_card_number'], name=request.POST['name']) name=request.POST['name'], ) ... In the above example, the values for the ``pass_word`` and Loading
docs/howto/static-files/deployment.txt +3 −3 Original line number Diff line number Diff line Loading @@ -106,7 +106,7 @@ Here's how this might look in a fabfile:: project.rsync_project( remote_dir=env.remote_static_root, local_dir=env.local_static_root, delete = True delete=True, ) .. _staticfiles-from-cdn: Loading
docs/intro/tutorial05.txt +9 −15 Original line number Diff line number Diff line Loading @@ -462,8 +462,7 @@ class: in the past, positive for questions that have yet to be published). """ time = timezone.now() + datetime.timedelta(days=days) return Question.objects.create(question_text=question_text, pub_date=time) return Question.objects.create(question_text=question_text, pub_date=time) class QuestionViewTests(TestCase): Loading Loading @@ -495,8 +494,7 @@ class: """ create_question(question_text="Future question.", days=30) response = self.client.get(reverse('polls:index')) self.assertContains(response, "No polls are available.", status_code=200) self.assertContains(response, "No polls are available.") self.assertQuerysetEqual(response.context['latest_question_list'], []) def test_index_view_with_future_question_and_past_question(self): Loading Loading @@ -580,10 +578,9 @@ in the future is not: The detail view of a question with a pub_date in the future should return a 404 not found. """ future_question = create_question(question_text='Future question.', days=5) response = self.client.get(reverse('polls:detail', args=(future_question.id,))) future_question = create_question(question_text='Future question.', days=5) url = reverse('polls:detail', args=(future_question.id,)) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_detail_view_with_a_past_question(self): Loading @@ -591,13 +588,10 @@ in the future is not: The detail view of a question with a pub_date in the past should display the question's text. """ past_question = create_question(question_text='Past Question.', days=-5) response = self.client.get(reverse('polls:detail', args=(past_question.id,))) self.assertContains(response, past_question.question_text, status_code=200) past_question = create_question(question_text='Past Question.', days=-5) url = reverse('polls:detail', args=(past_question.id,)) response = self.client.get(url) self.assertContains(response, past_question.question_text) Ideas for more tests -------------------- Loading
docs/ref/contrib/admin/index.txt +18 −15 Original line number Diff line number Diff line Loading @@ -615,10 +615,12 @@ subclass:: color_code = models.CharField(max_length=6) def colored_name(self): return format_html('<span style="color: #{};">{} {}</span>', return format_html( '<span style="color: #{};">{} {}</span>', self.color_code, self.first_name, self.last_name) self.last_name, ) class PersonAdmin(admin.ModelAdmin): list_display = ('first_name', 'last_name', 'colored_name') Loading Loading @@ -700,9 +702,11 @@ subclass:: color_code = models.CharField(max_length=6) def colored_first_name(self): return format_html('<span style="color: #{};">{}</span>', return format_html( '<span style="color: #{};">{}</span>', self.color_code, self.first_name) self.first_name, ) colored_first_name.admin_order_field = 'first_name' Loading Loading @@ -906,13 +910,11 @@ subclass:: def lookups(self, request, model_admin): if request.user.is_superuser: return super(AuthDecadeBornListFilter, self).lookups(request, model_admin) return super(AuthDecadeBornListFilter, self).lookups(request, model_admin) def queryset(self, request, queryset): if request.user.is_superuser: return super(AuthDecadeBornListFilter, self).queryset(request, queryset) return super(AuthDecadeBornListFilter, self).queryset(request, queryset) Also as a convenience, the ``ModelAdmin`` object is passed to the ``lookups`` method, for example if you want to base the Loading Loading @@ -1268,8 +1270,8 @@ subclass:: class PersonAdmin(admin.ModelAdmin): def view_on_site(self, obj): return 'https://example.com' + reverse('person-detail', kwargs={'slug': obj.slug}) url = reverse('person-detail', kwargs={'slug': obj.slug}) return 'https://example.com' + url Custom template options ~~~~~~~~~~~~~~~~~~~~~~~ Loading Loading @@ -1875,8 +1877,9 @@ provided some extra mapping data that would not otherwise be available:: def change_view(self, request, object_id, form_url='', extra_context=None): extra_context = extra_context or {} extra_context['osm_data'] = self.get_osm_info() return super(MyModelAdmin, self).change_view(request, object_id, form_url, extra_context=extra_context) return super(MyModelAdmin, self).change_view( request, object_id, form_url, extra_context=extra_context, ) These views return :class:`~django.template.response.TemplateResponse` instances which allow you to easily customize the response data before Loading