forked from markfinger/python-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
58 lines (48 loc) · 1.77 KB
/
Copy pathviews.py
File metadata and controls
58 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django_react.render import render_component
# An in-memory store of comment objects
comments = []
def index(request):
# Param flag to deactivate client-side Javascript
no_js = 'no-js' in request.GET
# Render the CommentBox component down to HTML
comment_box = render_component(
# The path to the component is resolved via Django's static-file
# finders
'components/CommentBox.jsx',
# We can pass data along to the component which will be
# accessible from the component via its `this.props` property
props={
'comments': comments,
'url': reverse('comment'),
'pollInterval': 2000,
},
# Ensure that the source code is translated to JS from JSX & ES6/7.
# This enables us to use future-facing JS in across the client-side
# and server-side
translate=True,
# If we intend to use React on the client-side, React will
# add extra attributes to the HTML so that the initial mount
# is faster, however these extra attributes are unnecessary
# if there is no JS on the client-side
to_static_markup=no_js
)
return render(request, 'index.html', {
'comment_box': comment_box,
'no_js': no_js,
})
def comment(request):
if request.POST:
comments.append({
'author': request.POST.get('author', None),
'text': request.POST.get('text', None),
})
if not request.is_ajax():
return redirect('index-no-js')
return HttpResponse(
json.dumps(comments),
content_type='application/json'
)