我正在使用带有reactjs的django,现在我已经维护会话并希望通过reactjs访问会话数据到cutomise UI。 我能为此做些什么?
//my models.py snippet
class Users(models.Model):
gender = models.CharField(max_length=200)
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state = models.CharField(max_length=30)
country = models.CharField(max_length=50)
bg = models.CharField(max_length=200)
badges = models.BigIntegerField(null=True)
dob = models.DateField()
contact = models.BigIntegerField(null=True)
age = models.BigIntegerField(default=0)
status = models.BigIntegerField(null=True)
//my views.py snippet
def login(request):
print(request.POST.get('username'))
username=str(request.POST.get('username'))
password=str(request.POST.get('password'))
if(Users.objects.filter(password=password, username=username).exists()):
request.session['username'] = username
name=request.session['username']
return render(request, 'loggedin.html', {"username" : name})
else:
return HttpResponse("incorrect data")
答案 0 :(得分:3)
这样做的一种方法是在GitHub上关注Scott Woodall的django-react-template 基本上,他的方法是将用户请求数据填充到Django' base.html' (在项目中称为app.html),这是由后端(Django)提供服务
在前端,他正在通过“窗口”访问数据。宾语。
{% load static %}
<!-- This is basically the ONLY webpage created in Django
see the window.django = {} part where variables are handed over initially to React
top most the Django CSRF Token
is this secure in Production?
...
...
-->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Django & React Starter</title>
...
...
</head>
<body>
<noscript>This application requires javascript to function.</noscript>
<div id="root"></div>
<script src="{% static "js/vendor.bundle.js" %}"></script>
<script>
window.django = {
csrf: "{{ csrf_token }}",
urls: {
logout: "{% url "logout" %}",
staticRoot: "{% static "" %}",
users: "{% url "emailuser-list" %}"
},
user: {
username: "{{ request.user.username }}",
full_name: "{{ request.user.get_full_name }}",
last_login: "{{ request.user.last_login }}",
permissions: immutable.Set(
{% autoescape off %}JSON.parse('{{ permissions }}'){% endautoescape %}
)
}
};
</script>
<script src="{% static "js/bundle.js" %}"></script>
</body>
</html>
Scott Woodall的所有学分o / 我希望能帮到你!