forbidden (csrf cookie not set.) django cbv code example

Example: django forbidden (csrf cookie not set.)

# Error: forbidden (csrf cookie not set.)

# ------------------------- SOLUTIONS ----------------------------- #

 1º - In your views.py file:

 def funtion(request):
    var = request.POST['name_from_element']  #instead of GET
	...
    

 2º - In your HTML:
  
 <form action="something" method="post">  # use post
  
  {% csrf_token %}          # ativate this token

  <label> Write a number: </label>
  <input type="number" name="num" /><br />
  <br />
  <input type="submit" /> <br />
</form>



# OR



 1º - In your views.py file, import:

 from django.views.decorators.csrf import csrf_exempt



 2º - Add the decorator to the function that is called:

 @csrf_exempt
 def funtion(request):
    ...