使用jQuery时,表单数据存储在数据库中

时间:2016-09-30 19:47:49

标签: jquery python django django-forms django-views

我试图使用jquery在下拉值变化时加载表单。在加载表单但提交数据后没有存储在数据库中。它只是刷新页面。

  

forms.py

class accountForm(forms.ModelForm):
    choice = (
        ('payment type','payment type'),
        ('paypal', 'paypal'),
        ('payeer', 'payeer'),
        ('payza', 'payza'),
        ('bitcoin', 'bitcoin'),
        )
    payment_type = forms.ChoiceField(choices = choice, widget = forms.Select(attrs = {'class':'forms-control'}))
    email = forms.EmailField(widget = forms.EmailInput(attrs={'class':'form-control'}))
    bitcoin = forms.CharField(widget = forms.TextInput(attrs = {'class':'form-control'}))
    amount = forms.IntegerField(widget = forms.NumberInput(attrs = {'class':'form-control'}))

    class Meta:
        model = payments
        fields = [
            "payment_type",
            "email",
            "bitcoin",
            "amount",

        ]
  

views.py

def payment(request, username):
    form = accountForm(request.POST or None)
    if form.is_valid():
        print(form)
        form.save(commit = False)
        form.save()
        return HttpResponseRedirect('/accounts/profile/')
    context = {'form':form}
    return render(request, 'payment.html', context)
  

payment.html

<div class="panel-body">
                      <div class="tab-content">
                        <div class="tab-pane active" id="home">
                          <div class="panel-body" id="panel">
                            <div class="form-group">
                              <h4 class="text-primary">Select Withdrawal Type</h4>
                              {{ form.payment_type }}
                            </div>
                          </div>
                          <div class="panel-body">
                            <form  method="POST" id="form" enctype='multipart/form-data'>
                            {% csrf_token %}

                            </form>
                          </div>
                        </div>
                      </div>
                    </div>
  

jquery的

<script type="text/javascript">
  $('select').on('change', function (e) {
    var Selected = $("option:selected", this);
    var valueSelected = this.value;
    console.log(valueSelected);
    if (valueSelected == 'paypal') {
      $("#panel").change(function (e) {
            $("#panel").empty();
            // $('#form').append('{% csrf_token %}');
            $('#form').append('<label>email</label>');
            $('#form').append('{{ form.email }}');
            $('#form').append('{{ form.amount }}');
            $('#form').append('<button class="btn btn-primary btn-default">submit</button>');

        })
    }
  });
</script>

1 个答案:

答案 0 :(得分:0)

forms.save()的实现在哪里?描述在表单模型中如何实现save()。它可能在某处或者如果您使用Amazon AWS和botto等任何服务作为界面,问题也可能出在这两个地方。

显然,您只打印了表单而且您正在使用HttpResponseRedirect,这就是为什么打印表单并重新加载您的个人资料页面。