Reputation: 107
Is there any full example on how Django rest framework can send session key to angular to save as cookie ??
Has been struggling for days...
I am running django on port 8000 and in angular ngserve I override httpclient to change the base url from 4200 to 8000 . However , it doesnt get any session,cookie data in the header
Upvotes: 0
Views: 855
Reputation: 17514
I worked with Django and AngularJS for sometime. What we did was as below:
index.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>
In your views.py file:
@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")
The SiteController
had ng-init
which was executed as very 1st thing before any other controller and page was loaded. We had ecommerce
site so, the user had to login
and logout
and we maintained their activity using session.
If you have some different scenario or you need more help then elaborate your question more.
Upvotes: 1