0

I'm using Django-rest-framework

views.py:

@api_view(['GET'])
def addToCartForSession(request, pk):
    product = get_object_or_404(Product, pk=pk)

    if not request.session.exists(request.session.session_key):
        request.session.create() 

    mycart, __ = Cart.objects.get_or_create(session_key=request.session.session_key)
    mycart.product.add(product)

    return Response({'response':'ok'})

When this function is called, it create a new key. and for new key, it create a new Cart.

But every single user should have only one cart.

So, How can I stop creating new session_key for a single user? but one session_key will be created if there is no session_key. how can i do that?

also I want to add that:

@api_view(['GET'])
def addToCartForSession(request, pk):

    print(request.session.session_key) #>>> None
    return Response({'response':'ok'})

when I make this, It prints None.

in my Front-end (Reactjs):

    addToCart=()=>{
        var id = this.props.id
        
            var url2 = 'http://127.0.0.1:8000/addToCartForSession/'+id+'/'
            fetch(url2,{
                method:'GET',
                headers: {
                    'Content-Type': 'application/json',
                }
            }).then(res=>res.json().then(result=>{
                if(result.response === 'ok'){
                    this.props.dispatch({
                        type: 'itemInCart',
                    })
                    this.setState({addedToCart: true})
                }
            }))
        }
    

this functionaddToCartForSession called several time (when i add an item to the cart). as long as this function called, new session_key/cart creates. but it will be only one cart for a single user.

Asif Biswas
  • 161
  • 2
  • 9
  • You might not be maintaining a session from your client / frontend. Please [edit] your question and add the code on how you make requests and also add the relevant tags to the question. – Abdul Aziz Barkat Jun 06 '21 at 17:15
  • @AbdulAzizBarkat, sir, I added my front-end code. can you fix the error please? – Asif Biswas Jun 06 '21 at 17:36
  • Does this answer your question? [Fetch API with Cookie](https://stackoverflow.com/questions/34558264/fetch-api-with-cookie) You need to use `credentials: 'include'` in your call to `fetch` so that the cookies, etc. are included in the requests you make to the server. – Abdul Aziz Barkat Jun 06 '21 at 17:38
  • @AbdulAzizBarkat, sir, new problem happened . I added ```credentials: 'include',``` in fetch. And now, if i call the function, it shows>>> Credentials flag is true, but Access-Control-Allow-Credentials is not "true".<<< how can i fix this? – Asif Biswas Jun 06 '21 at 17:50
  • See this question: https://stackoverflow.com/questions/22355540/access-control-allow-origin-in-django-app-when-accessed-with-phonegap (Second time I am linking this question today ;), another person had a similar problem) – Abdul Aziz Barkat Jun 06 '21 at 17:53
  • @AbdulAzizBarkat, sir, I couldn't solve the problem. I think we are solving it in wrong way. My simple question is: How can I use **session_key** in drf? – Asif Biswas Jun 06 '21 at 18:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233456/discussion-between-asif-biswas-and-abdul-aziz-barkat). – Asif Biswas Jun 07 '21 at 16:35

0 Answers0