如何将json的数据放入表单中?

时间:2017-12-06 07:06:46

标签: javascript python html django

我想把json的数据放到form.I写在index.html

      {% load staticfiles %}
        <html lang="ja">
          <head>
            <meta charset="utf-8">
            <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
            <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
            <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
          </head>
          <body>
        
          <main>
        
            <form class="form-horizontal" action="/accounts/regist_save/" method="POST">
            
            
        
            
            
                <label for="id_email">Email</label>
                
             {{ regist_form.email }}    
                
            
     
        
              {% include 'registration/sex.html' %}
        
        
                <button type="submit" class="regist">REGIST</button>
            
                <input name="next" type="hidden"/>
            
        
              {% csrf_token %}
            
            </form>
            
        
          </main>
        
          </body>
        </html>

在forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import AuthenticationForm
from .models import User
from .models import NewUser

class RegisterForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('email',)
    def __init__(self, *args, **kwargs):
        super(RegisterForm, self).__init__(*args, **kwargs)
        self.fields['email'].widget.attrs['class'] = 'form-control'

class ProfileForm(forms.ModelForm):
    class Meta:
        model = NewUser
        fields = (
             "sex"
        )

在models.py

#coding:utf-8
from django.db import models
from django.contrib.auth.models import User

class NewUser(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    sex = models.CharField(max_length=100,null=True, blank=True, default=None)

in sex.html

    <form class="form-horizontal" method="post" action="#">
        <div class="form-group-lg">
        <label for="sex">SEX</label>
        
        <select class="form-control sex" name="sex">
        <option value="">--</option>
        <option value="male">MAN</option>
        <option value="female">FEMALE</option>
        </select>
        </div>
        
        
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        function showSex() {
            var num = document.forms[0].sex.selectedIndex;
            var sex = document.forms[0].sex.options[num].value;
            console.log(sex);
        }
    
        document.addEventListener("DOMContentLoaded", showSex);
        for (var i = 0, e = document.querySelectorAll(".form-horizontal select"); i < e.length; i++) {
            e[i].addEventListener("change", showSex);
        }
    </script>

现在REGIST按钮无法发送,所以我无法注册这些数据。为什么会发生这种情况?我想把var sex数据以表格形式注册。我该如何解决这个问题? 我如何将js数据发送到python?这是不可能的吗?我想我应该将javascript数据转换为python数据,但我无法理解如何做到这一点。

0 个答案:

没有答案