0

im working on django-angular5 project i have a login form in angular how should i add csrf token to my form ? without what i get this error i searched same topic but people are using angular js that will not help me (i ng build angular app and im using static files)

Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
    CSRF token missing or incorrect.

it is my simple form for login in home.compoenent.html

<form method="post">

    <div class="input-field col s6">
        <input id="first_name" type="text" name="username" class="validate">
        <label for="first_name">نام کاربری</label>
    </div>
   <div class="input-field col s6">
        <input  id="first_name" type="password" name="password" class="validate">
        <label for="first_name">کلمه عبور</label>
    </div>

    <input type="submit" class="waves-effect waves-light btn" value="login"/>
    <input type="hidden" name="next" value="{{ next }}"/>
</form>
moh
  • 433
  • 10
  • 33

1 Answers1

-1

in your form you should just add {% csrf_token %} you'll have something like this :

    <form method="post">
        {% csrf_token %}
        <div class="input-field col s6">
            <input id="first_name" type="text" name="username" class="validate">
            <label for="first_name">نام کاربری</label>
        </div>
       <div class="input-field col s6">
            <input  id="first_name" type="password" name="password" class="validate">
            <label for="first_name">کلمه عبور</label>
        </div>

        <input type="submit" class="waves-effect waves-light btn" value="login"/>
        <input type="hidden" name="next" value="{{ next }}"/>
    </form>
YGouddi
  • 341
  • 2
  • 14
  • we cant use {% %} (jango template tag) there we will get this error `vendor.bundle.js:14136 Uncaught Error: Template parse errors: Unexpected character "EOF" (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.) (" ` – moh Dec 26 '17 at 13:31
  • then check [this](https://stackoverflow.com/questions/18156452/django-csrf-token-angularjs) – YGouddi Dec 26 '17 at 13:33
  • that is about angular js its quite diffrent with angular 4 or 5 – moh Dec 26 '17 at 13:37