Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用django获取上下文响应_Python_Django_Reactjs - Fatal编程技术网

Python 如何使用django获取上下文响应

Python 如何使用django获取上下文响应,python,django,reactjs,Python,Django,Reactjs,我需要使用django获取react中的上下文 但是我做不到 这是我的密码 在我的jsx中 <h1>{{titulo}}</h1> <h2>{{ejemplo}}</h2> 但未渲染,出现错误:( 未捕获引用错误:未定义titulo仅当模板在服务器上呈现时,Django上下文才可用。React使用Javascript,这意味着它在浏览器上呈现。如果要在React应用程序中使用Django变量,则需要在Javascript中设置这些变量(请确保在导

我需要使用django获取react中的上下文 但是我做不到

这是我的密码 在我的jsx中

<h1>{{titulo}}</h1>
<h2>{{ejemplo}}</h2>
但未渲染,出现错误:(


未捕获引用错误:未定义titulo

仅当模板在服务器上呈现时,Django上下文才可用。React使用Javascript,这意味着它在浏览器上呈现。如果要在React应用程序中使用Django变量,则需要在Javascript中设置这些变量(请确保在导入
bundle.js
之前执行此操作)

因此,您的模板可能如下所示:

{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body >
<div id="app"></div>

<script type="text/javascript">
  /* Put your context vars here. I recommend putting them under 
     a variable to avoid naming conflicts. 
  */
  var context = {
    titulo: '{{titulo}}',
    ejemplo: '{{ejemplo}}'
  };
</script>

<script type="text/javascript" src="{% static 'bundle.js' %}"></script>
</body>
</html>
{%load staticfiles%}
指数
/*把你的上下文变量放在这里。我建议把它们放在下面
避免命名冲突的变量。
*/
变量上下文={
titulo:“{{titulo}}”,
ejempo:“{{ejempo}}”
};
然后在React中,您可以引用
context.variable
来获得所需的值

def registro (request,giro):
    reg = 'Registro Normal'
    if giro==1:
        reg='Registro Especial'

    context = {
        'ejemplo':'tests',
        'titulo':reg
    }
    return render(request,'home/registro.html',context)
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body >
<div id="app"></div>

<script type="text/javascript">
  /* Put your context vars here. I recommend putting them under 
     a variable to avoid naming conflicts. 
  */
  var context = {
    titulo: '{{titulo}}',
    ejemplo: '{{ejemplo}}'
  };
</script>

<script type="text/javascript" src="{% static 'bundle.js' %}"></script>
</body>
</html>