简体   繁体   中英

django inserting data into db using html template

I'm new in django. I'm trying to connect already made an html file to django backend without rebuilding whole file. Already created forms and views in python but have no idea what to put into html file.

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:

    <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. 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

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 .

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.

Now you have to fetch data from form and perform the validation and checks by yourself in views. So the post would be

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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