Django:将JS字典发送到Django视图

时间:2018-12-17 04:18:40

标签: javascript python django

所以,我有一个JS字典,我想将其发送到Django View,因为我需要使用它使用python库来调用函数。

我需要发送:dir_charge到我的Django视图中,但这是在名为culqi()的JS函数内部,该函数负责在字典中创建source_id字段,并且email字段:

js:

此功能culqi()在问题末尾从表单中捕获数据。

<script>

   function culqi() {
      if (Culqi.token) { // ¡Objeto Token creado exitosamente!
      var dir_charge = {
              amount: 24,
              currency_code: "PEN",
              email: Culqi.token.email,
              source_id: Culqi.token.id
                    };
      } else { // ¡Hubo algún problema!
              // Mostramos JSON de objeto error en consola
              console.log(Culqi.error);
              alert(Culqi.error.user_message);
             }
      };

</script>

当用户单击此按钮时,将生成JS:

html:

<button id="buyButton">Pagar</button>

感谢其他JS:

<script>

            $('#buyButton').on('click', function (e) {
                // Abre el formulario con la configuración en Culqi.settings
                Culqi.open();
                e.preventDefault();
            });

</script>

当用户单击他的按钮时,将显示此表单(它不是在HTMl中进行硬编码的,而是从外部JS库调用的):

<form class="form">
    <div class="form-row">
        <div class="col col-12">
            <div class="form-group">
                <div class="input">
                    <input name="cardNumber" autocomplete="cc-number" type="tel" maxlength="19" class="input-card"
                           data-mask="#### #### #### ####" data-previous-value="">
                </div>
                <label for="cardNumber" class="">Número de Tarjeta</label>
                <div class="brand"></div>
            </div>
        </div>
    </div>
    <div class="form-row">
        <div class="col col-6">
            <div class="form-group">
                <div class="input">
                    <input name="cardExp" autocomplete="cc-exp" type="tel" maxlength="5" class="input-card"
                           data-mask="##/##" data-previous-value="">
                </div>
                <label for="cardExp" class="">Mes / Año</label>
            </div>
        </div>
        <div class="col col-6">
            <div class="form-group">
                <div class="input">
                    <input name="cardCVV" autocomplete="cc-csc" type="tel" maxlength="3" class="input-card"
                           data-mask="###" data-previous-value="">
                </div>
                <label for="cardCVV" class="">CVV</label>
                <a>(?)</a>
            </div>
        </div>
    </div>
    <div class="form-row" style="display: none;">
        <div class="col col-12">
            <div class="form-group">
                <div class="input">
                    <select name="custom-cuotas" id="" class="">
                        <option value="" selected="selected" disabled="disabled" hidden="hidden">Pagar en Cuotas
                        </option>
                        <option value="1">Sin Cuotas</option>
                        <option value="2">2 Cuotas</option>
                        <option value="4">4 Cuotas</option>
                        <option value="6">6 Cuotas</option>
                        <option value="8">8 Cuotas</option>
                        <option value="10">10 Cuotas</option>
                        <option value="12">12 Cuotas</option>
                        <option value="3">3 Cuotas</option>
                        <option value="5">5 Cuotas</option>
                        <option value="7">7 Cuotas</option>
                        <option value="9">9 Cuotas</option>
                        <option value="24">24 Cuotas</option>
                        <option value="48">48 Cuotas</option>
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div class="form-row">
        <div class="col col-12">
            <div class="form-group">
                <div class="input">
                    <input name="cardEmail" autocomplete="email" type="email" class="input input-card">
                </div>
                <label for="cardEmail" class="">Correo Electrónico</label>
            </div>
        </div>
    </div>
    <div class="form-pay">
        <button type="submit" class="btnAction">
            <span>Pagar </span>
            <span class="payText PEN">.24</span>
            <small>PEN</small>
        </button>
    </div>
</form>

更新1:

urls.py:

从django.urls导入路径 来自。导入视图

app_name ='购物车'

urlpatterns = [
    path('', views.cart_detail, name = 'cart_detail'),
    path('full_remove/<int:cart_item_id>/', views.full_remove, name = 'full_remove'),
]

views.py

def cart_detail(request):
    print(amount)
    print(currency_code)
    print(email)
    print(source_id)
    pass

1 个答案:

答案 0 :(得分:0)

如评论中所述,您可能对Ajax感兴趣,希望您使用的是jQuery,您可以使用类似的

<script>

   function culqi() {
      if (Culqi.token) { 
       $.ajax({
              url: "your_url",
              data: {
              amount: 24,
              currency_code: "PEN",
              email: Culqi.token.email,
              source_id: Culqi.token.id
           }
              }).done(function() {
               // do something with scess
               $( this ).addClass( "done" );
              });
      } else { 
              console.log(Culqi.error);
              alert(Culqi.error.user_message);
             }
      };

</script>