我按照permitted content示例使用django设置了create-react-app。网页在这样的视图中传递:
def get(self, request):
try:
with open(os.path.join(settings.REACT_APP_DIR, 'build', 'index.html')) as f:
return HttpResponse(f.read())
我正在尝试将conext(conext = {''foo':'bar'})传递给它。
我通过this尝试过:
class MyView(DetailView):
"""
Serves the compiled frontend entry point (only works if you have run `yarn
run build`).
"""
def get(self, request):
try:
with open(os.path.join(settings.MY_VIEW_DIR, 'build', 'index.html')) as f:
return HttpResponse(f.read())
except FileNotFoundError:
return HttpResponse(
"""
This URL is only used when you have built the production
version of the app. Visit http://localhost:3000/ instead, or
run `yarn run build` to test the production version.
""",
status=501,
)
def get_context_data(self, *args, **kwargs):
context = super(MyView. self).get_context_data(*args, **kwargs)
context['message'] = 'Hello World!'
return context
我还尝试将网页变成模板并返回
return render(request, 'path/to/my/index.html', {'foo':'bar'})
但这只是返回没有我的反应代码的页面。
是否有更好的方法用Django实现create-react-app或将react代码转换为模板的方法?