示例 django session 和带有 django rest 框架的 angular cookie



有没有关于 Django rest 框架如何将会话密钥发送到 angular 以另存为 cookie 的完整示例?

已经挣扎了好几天...

我在端口 8000 上运行 django,在角度 ngserve 中,我覆盖了 httpclient 以将基本网址从 4200 更改为 8000。但是,它不会在标头中获取任何会话,cookie数据

我使用Django和AngularJS已经有一段时间了。我们所做的如下:

索引.html

{% load google_analytics %}
{% load seo %}
{% load staticfiles %}
<!DOCTYPE html>
<html ng-app="myApp" lang="en">
<head>
  <!-- ur code -->
</head>
<script src="{% static "JS/external/angular.js" %}"></script>
<body ng-controller="SiteController">
  {{ my_meta.heading }}
  <div ng-init="init('{{ session_key }}','{{ some_other_params }}')">
   <!-- your site code here -->
  </div>
</body>
</html>

views.py 文件中:

@csrf_protect
@requires_csrf_token
def index(request):
    if request.session.session_key is None:
        request.session['has_session'] = True
        request.session.modified = True
    return render(request, 'index.html', {'session_key': request.session.session_key,
                                           ..... params which are to be provided before index.html has been rendered ......
                                          }
                  , content_type="text/html")

SiteController具有ng-init,在加载任何其他控制器和页面之前,它作为第一件事执行。我们有ecommerce网站,所以用户必须loginlogout,我们使用会话来维护他们的活动。

如果您有一些不同的情况或需要更多帮助,请详细说明您的问题。

最新更新