简体   繁体   English

Django使用HTML模板将数据插入数据库

[英]django inserting data into db using html template

I'm new in django. 我是django新手。 I'm trying to connect already made an html file to django backend without rebuilding whole file. 我正在尝试将已经制作的html文件连接到django后端,而不重建整个文件。 Already created forms and views in python but have no idea what to put into html file. 已经在python中创建了表单和视图,但不知道将什么放入html文件。

view class: 查看类别:

class signup(View):
    template = loader.get_template('signup.html')
    form_class = UserRegistrationForm

    def get(self, request):
        form = self.form_class(None)
        return render(request, 'signup.html', {'form': form})

    def post(self, request):
        form = self.form_class(request.POST)

        if form.is_valid():
            current_user = form.save(commit=False)

            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            current_user.set_password(password)
            current_user.save()

            userToAuthenticate = authenticate(email=email, password=password)

            if userToAuthenticate is not None:
                if userToAuthenticate.is_active:
                    login(request, userToAuthenticate)
                    return redirect('siteViews:index')

        return render(request, 'signup.html', {'form': form})

form code: 表格代码:

class UserRegistrationForm(forms.ModelForm):

    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['email', 'password']

and html code: 和html代码:

    <div id="registersquare">
        <div id="panel">
            <form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                <label for="email">Email adress:</label>
                <input type="email" id="username" name="email}">

                <label for="password">Password:</label>
                <input type="password" id="password" name="password">

                <label for="password">Repeat password:</label>
                <input type="password" id="password" name="repeatedpassword">

                <label class="control-label col-sm-2" for="password">{{ field.label_tag }}</label>

                <div id="lower">
                    <input type="checkbox"><label class="check" for="checkbox"><a style="color: #999999;" href="#">I Accept Website Terms And Conditions.</a></label>
                    <input type="submit" value="Sign up">
                </div>
            </form>
        </div>
    </div>

anyone can explain how to do it? 任何人都可以解释该怎么做? cheers 干杯

You need to delete the labels and inputs from your html file and add this tag after the {% csrf_token %} , {{form.as_p}} , that's a start. 您需要从html文件中删除标签和输入,并在{% csrf_token %}{{form.as_p}}之后添加此标签,这是一个开始。 You are also using an older version of Django, the way I can tell is because when you defined your ModelForm you wrote forms.ModelForm when it has been changed to just ModelForm, to upgrade write 您也使用Django的旧版本,我可以告诉的方式是因为当你定义你的ModelForm你写forms.ModelForm时,它已被更改为刚刚的ModelForm,升级写

pip install -U Django

You essentially created two forms, one with just html and one with Django only you did not apply your ModelForm to your html file instead you just made a html form instead of a html rendered Django ModelForm . 本质上,您创建了两种形式,一种仅包含html,一种仅使用Django,您没有将ModelForm应用于html文件,而是仅创建了html表单,而不是html呈现的Django ModelForm

You hav already created a Form, which is not Django's form, so you dont actually have to write anything in forms.py , as the purpose of it is to create an form based on the model structure and perform validations according to the fields defined. 您已经创建了一个Form,它不是Django的表单,因此您实际上不必在forms.py编写任何forms.py ,因为它的目的是基于模型结构创建表单并根据定义的字段执行验证。

Now you have to fetch data from form and perform the validation and checks by yourself in views. 现在,您必须从表单中获取数据,并亲自在视图中执行验证和检查。 So the post would be 因此,该post将是

def post(self, request):
    email = request.POST.get('email')  # get value in name="email" field
    password = request.POST.get('password')
    repeatedpassword = request.POST.get('repeatedpassword')

    if password == repeatedpassword: # manual validation to check if both string are same
        # Other Validations code here and
        # Register or Login etc functions here

    return render(request, 'signup.html', {'form': form})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM