title: Generate Django JWT token from inside a view
body: <p>I'm working on a Django(1.10) project and created an api for my project using Django Rest Framework.I need to generate JWT token from inside my view. </p>
  
  <p>Here's what I have tried:</p>
  
  <p>Settings for REST Framework in <strong>settings.py</strong>:</p>
  
  <pre><code>REST_FRAMEWORK = {
  # Use Django's standard `django.contrib.auth` permissions,
  # or allow read-only access for unauthenticated users.
  'DEFAULT_PERMISSION_CLASSES': [
      'rest_framework.permissions.DjangoObjectPermissions'
  ],
  'DEFAULT_AUTHENTICATION_CLASSES': (
      'rest_framework.authentication.SessionAuthentication',
      'rest_framework.authentication.BasicAuthentication',
      'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
  ),
  }
  
  AUTHENTICATION_BACKENDS = (
      'django.contrib.auth.backends.ModelBackend',
      'allauth.account.auth_backends.AuthenticationBackend',
  )
  </code></pre>
  
  <p>Here is the URL to obtain a token manually:</p>
  
  <pre><code>    url(r'^api-token-auth/', obtain_jwt_token)
  </code></pre>
  
  <p>Here is the template from which user will request to obtain a token:</p>
  
  <pre><code>        &lt;div class="col-md-9 content"&gt;
          &lt;div class="world-map" style="width: 100%; margin-left:2%"&gt;
              &lt;h3&gt; Get your Authorization Token to use REST Api :&lt;/h3&gt;
          &lt;/div&gt;
          &lt;div class="col-lg-6" style="margin-top: 20%;  margin-left: 25%;"&gt;&lt;br&gt;
              &lt;form action="{% url 'users:getApiAuthToken' %}" method="post" id="getTokenForm"&gt;
                  {% csrf_token %}
                  &lt;a href="" style="text-decoration: none; color: white;"&gt;
                  &lt;button type="submit" class="btn waves-effect waves-light  pull-right" value="Submit" id="enableApi" style="padding-right: 10%; font-size: 20px;"&gt;&lt;i
                              class="material-icons left"&gt;casino&lt;/i&gt;  Generate Api Token!&lt;/button&gt;&lt;/a&gt;
              &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And here's how the ajax request will be sent:</p>
  
  <pre><code>    $(document).on('submit', '#getTokenForm', function (e) {
      e.preventDefault();
      $.ajax({
          url: '{% url 'users:getApiAuthToken' %}',
          type: 'POST',
          data: {
              csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
          },
          success: function (data) {
              console.log(data);
          }
      });
  });
  </code></pre>
  
  <p>And here is my view where this request will process:</p>
  
  <pre><code>        if request.method == 'POST':
              post_data = request.POST.copy()
              post_data.update({'user': request.user.pk})
              payload = {'username': request.user.username, 'password': request.user.password}
              r = requests.post('http://127.0.0.1:8000/api-token-auth/', params=payload)
              return HttpResponse('Get token auth request and data is as: {}'.format(r.text))
  </code></pre>
  
  <p>But it returns as:</p>
  
  <blockquote>
    <p>Get token auth request and data is as: {"username":["This field is required."],"password":["This field is required."]}</p>
  </blockquote>
  
  <p>How can I pass current user's credentials to generate a token for him?
  I need to genearte a token and send back to the user then he will be able to use this token for our rest api. How can i genearte this token inside this view?</p>
  
  <p>Help me, please!</p>
  
  <p>Thanks in advance!</p>