简体   繁体   English

禁止使用Django 403

[英]Django 403 forbidden

Django 1.9.5 When I press the button on the console of my browser displayed an error " http://127.0.0.1:8000/encrypt [HTTP/1.0 403 Forbidden 4мс]" What is the problem? Django 1.9.5当我按浏览器控制台上的按钮时,显示错误“ http://127.0.0.1:8000/encrypt [HTTP / 1.0 403 Forbidden4мс]”这是什么问题?

home.html home.html

$(document).ready(function() {
    $("#encrypt").click(function () {
        var postData = {
            text: $("#input-box").val(),
            rotate: $("#rotate").val()
        };
        $.post('encrypt', postData);
        return false;
    });
});
  </script>

  <div class="container">
    <legend>Caesar cipher</legend>
    <div class="row">
      <form name="ciepher" method="POST" action="">
        {% csrf_token %}
        <!--form code -->
         <button class="btn" name="encrypt" id="encrypt"><span class="icon-arrow-right"></span></button>
      </form>

urls.py: urls.py:

urlpatterns = [
    url(r'^$', views.home, name="home"),
    url(r'^encrypt$', views.encrypt, name="encrypt")
]

views.py: views.py:

def home(request):
    return render_to_response("home.html", context_instance = RequestContext(request))

def encrypt(request):
    input_text = request.POST["text"]
    rotate = request.POST["rotate"]
    output_text = models.encode(input_text, rotate)
    frequency = models.get_frequency(input_text)
    return render_to_response("home.html", {'input_text': input_text, 'rotate': rotate, 'output_text': output_text, 'frequency': frequency}, context_instance = RequestContext(request))

Thank you 谢谢

You're not sending the CSRF token. 您没有发送CSRF令牌。 You have it in your form, but you're posting data via Ajax not via a normal form submission; 您已将其保存在表单中,但是您是通过Ajax发布数据的,而不是通过常规表单提交的。 you need to include the token in the Ajax post data. 您需要在Ajax发布数据中包含令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM