Sunday 28 July 2013

django's 'post' metod: about csrf protection

The CSRF middleware in django provides easy-to-use protection against Cross Site Request Forgeries. it is also regards as default way of execution

how it works

1, the middle ware 'django.middleware.csrf.CsrfViewMiddleware' to your list of middleware classes.

2, On server side the csrf is csrf_token like:
<form action="." method="post">{% csrf_token %}
You may need manually update the csrf by:
    
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
    
def my_view(request):
    c = {}
    c.update(csrf(request))
    return render_to_response("a_template.html", c)
 
3,if you already added the the csrf middleware and you do not want to apply it, you should added
@csrf_exempt
 
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_view(request):
    return HttpResponse('Hello world') 
it is very import to added @csrf_exempt if you want to use ajax in django framework.

10 comments:

  1. This comment has been removed by the author.

    ReplyDelete

  2. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.

    angularjs Training in bangalore

    angularjs Training in electronic-city

    angularjs Training in online

    angularjs Training in marathahalli

    ReplyDelete
  3. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    python interview questions and answers | python tutorials

    ReplyDelete
  4. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Java training in Chennai | Java training in Bangalore

    Java interview questions and answers | Core Java interview questions and answers

    ReplyDelete
  5. I enjoy what you guys are usually up too. This sort of clever work and coverage! Keep up the wonderful works guys I’ve added you guys to my blog roll.
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  6. Your blog is very interesting. Your level of thinking is good and the clarity of writing is excellent. I enjoyed reading this post. I am also a blogger, You can visit our services here Android App Development Services
    You can also contact here, if you are looking forward to Hire Android App Developers or best Mobile App Development Company

    ReplyDelete