Skip to content

Commit 2edadca

Browse files
committed
add responsenotmodified django example code
1 parent 3eae94a commit 2edadca

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

content/pages/examples/django/django-http-httpresponsebadrequest.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebartitle: django.http HttpResponseBadRequest
77
meta: Example Python code for using the HttpResponseBadRequest object provided by Django in the django.http module.
88

99

10-
[HttpResponseBadRequest](https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.HttpResponseBadRequest)
10+
[HttpResponseBadRequest](https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpResponseBadRequest)
1111
([source code](https://github.com/django/django/blob/master/django/http/response.py))
1212
returns the
1313
[HTTP 400 status code](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)

content/pages/examples/django/django-http-httpresponseforbidden.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ toc: False
66
sidebartitle: django.http HttpResponseForbidden
77
meta: Example Python code for using the HttpResponseForbidden object provided by Django in the django.http module.
88

9+
910
[HttpResponseForbidden](https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpResponseForbidden)
1011
([source code](https://github.com/django/django/blob/master/django/http/response.py))
1112
returns the 403 status code to an inbound HTTP request in a
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
title: django.http HttpResponseNotModified Python Code Examples
2+
category: page
3+
slug: django-http-httpresponsenotmodified-examples
4+
sortorder: 50063
5+
toc: False
6+
sidebartitle: django.http HttpResponseNotModified
7+
meta: Example Python code for using the HttpResponseNotModified object provided by Django in the django.http module.
8+
9+
10+
[HttpResponseNotModified](https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpResponseNotModified)
11+
([source code](https://github.com/django/django/blob/master/django/http/response.py))
12+
returns the
13+
[HTTP 304 status code](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
14+
from a [Django](/django.html) web application view. The HTTP 304 status code
15+
indicates that the resource has not been modified since the client last requested
16+
it.
17+
18+
[HttpResponseRedirect](/django-http-httpresponseredirect-examples.html)
19+
and
20+
[HttpResponsePermanentRedirect](/django-http-httpresponsepermanentredirect-examples.html)
21+
are other types of 300-level HTTP status codes that can be
22+
sent as a response by your Django application.
23+
24+
25+
## Example 1 from wagtail
26+
[wagtail](https://github.com/wagtail/wagtail)
27+
([project website](https://wagtail.io/)) is a fantastic
28+
[Django](/django.html)-based CMS with code that is open source
29+
under the
30+
[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
31+
32+
[**wagtail / wagtail / utils / sendfile_streaming_backend.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/utils/sendfile_streaming_backend.py)
33+
34+
```python
35+
# Sendfile "streaming" backend
36+
# This is based on sendfiles builtin "simple" backend but uses a StreamingHttpResponse
37+
38+
import os
39+
import re
40+
import stat
41+
from email.utils import mktime_tz, parsedate_tz
42+
from wsgiref.util import FileWrapper
43+
44+
~~from django.http import HttpResponseNotModified, StreamingHttpResponse
45+
from django.utils.http import http_date
46+
47+
48+
def sendfile(request, filename, **kwargs):
49+
# Respect the If-Modified-Since header.
50+
statobj = os.stat(filename)
51+
52+
if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
53+
statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
54+
~~ return HttpResponseNotModified()
55+
56+
response = StreamingHttpResponse(FileWrapper(open(filename, 'rb')))
57+
58+
response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
59+
return response
60+
61+
62+
def was_modified_since(header=None, mtime=0, size=0):
63+
"""
64+
Was something modified since the user last downloaded it?
65+
66+
header
67+
This is the value of the If-Modified-Since header. If this is None,
68+
I'll just return True.
69+
70+
mtime
71+
This is the modification time of the item we're talking about.
72+
73+
size
74+
This is the size of the item we're talking about.
75+
"""
76+
try:
77+
if header is None:
78+
raise ValueError
79+
matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header,
80+
re.IGNORECASE)
81+
header_date = parsedate_tz(matches.group(1))
82+
if header_date is None:
83+
raise ValueError
84+
header_mtime = mktime_tz(header_date)
85+
header_len = matches.group(3)
86+
if header_len and int(header_len) != size:
87+
raise ValueError
88+
if mtime > header_mtime:
89+
raise ValueError
90+
except (AttributeError, ValueError, OverflowError):
91+
return True
92+
return False
93+
94+
```
95+

content/pages/examples/django/django-http-httpresponseredirect.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebartitle: django.http HttpResponseRedirect
77
meta: Example Python code for using the HttpResponseRedirect object provided by Django in the django.http module.
88

99

10-
[HttpResponseRedirect](https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.HttpResponseRedirect)
10+
[HttpResponseRedirect](https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpResponseRedirect)
1111
is a subclass of
1212
[HttpResponse](/django-http-httpresponse-examples.html)
1313
([source code](https://github.com/django/django/blob/master/django/http/response.py))

content/pages/meta/00-change-log.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ on GitHub.
2626
* [django.http HttpResponse](/django-http-httpresponse-examples.html)
2727
* [django.http HttpResponseBadRequest](/django-http-httpresponsebadrequest-examples.html)
2828
* [django.http HttpResponseForbidden](/django-http-httpresponseforbidden-examples.html)
29+
* [django.http HttpResponseNotModified](/django-http-httpresponsenotmodified-examples.html)
2930
* [django.http HttpResponseRedirect](/django-http-httpresponseredirect-examples.html)
3031
* [django.template.response TemplateResponse](/django-template-response-templateresponse-examples.html)
3132
* [django.template.response SimpleTemplateResponse](/django-template-response-simpletemplateresponse-examples.html)

theme/templates/table-of-contents.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ <h4 class="bp">django.http
261261
<a href="/django-http-httpresponse-examples.html">HttpResponse</a>
262262
<a href="/django-http-httpresponsebadrequest-examples.html">HttpResponseBadRequest</a>
263263
<a href="/django-http-httpresponseforbidden-examples.html">HttpResponseForbidden</a>,
264+
<a href="/django-http-httpresponsenotmodified-examples.html">HttpResponseNotModified</a>,
264265
<a href="/django-http-http404-examples.html">Http404</a>,
265266
<a href="/django-http-httpresponsepermanentredirect-examples.html">HttpResponsePermanentRedirect</a>,
266267
<a href="/django-http-httpresponseredirect-examples.html">HttpResponseRedirect</a>

0 commit comments

Comments
 (0)