我想通过验证sqlite3数据库中是否存在该用户来创建注册表单以注册用户

时间:2018-11-27 06:50:42

标签: javascript python django

我想通过验证sqlite3数据库中是否存在该用户来创建注册表单来注册用户。

这是我的根urls.py文件:

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('orphanageoperations.urls')),
]

子网址文件:

from django.urls import path
from .import views

urlpatterns=[
        path('',views.orphanage_main_page,name='orphanage_main_page'),
        path('signup.html',views.signup,name='signup'),
        path('login.html',views.login,name='login')
        ]

views.py文件

from django.shortcuts import render
from .forms import SignupForm
def signup(request):
    if request.method=='POST':
        form=SignupForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
    else:
        form=SignupForm()

    return render(request,'orphanageoperations/signup.html',{})

models.py

from django.db import models
from django.utils import timezone
from django.conf import settings
# Create your models here.

class Signup(models.Model):
    username=models.CharField(max_length=20);
    usermail=models.CharField(max_length=20,primary_key=True);
    userph=models.CharField(max_length=13);
    userpwd=models.CharField(max_length=20);

    def createUser(self):
        self.username;
        self.usermail;
        self.userph;
        self.userpwd;

forms.py

from django import forms
from .models import Signup

class SignupForm(forms.ModelForm):
    class Meta:
        model= Signup;
        fields=('username','usermail','userph','userpwd',);

signup.html

<!DOCTYPE html>
<html>
<head>
    <title>Signup</title>
    <meta charset="uts-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script>
        function validate_signup_form(){
            var username=document.getElementById("name").value;
            var mobilenum=document.getElementById("phno").value;
            var mailid=document.getElementById("userMail").value;
            var password=document.getElementById("pwd").value;
            if(username==""){
            alert("Please Enter the username");
            return false;
            }
            if(mobilenum==""){
            alert("Please Enter mobile number");
            return false;
            }
            if(mailid==""){
            alert("Plese enter your mail id");
            return false;
            }
            if(password==""){
            alert("Plese enter your password");
            return false;
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <br />
        <h4 style="padding-left:5em">Signup Form</h4>
        <form name="signupForm" action="signup" class="form-horizontal" onsubmit="return validate_signup_form()" method="post">{% csrf_token %}
            <div class="form-group">
                <label class="control-label col-sm-2" for="name">Name:</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" id="name" placeholder="Enter your name" name="name">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2" for="phno">Mobile:</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" id="phno" placeholder="Enter your mobile number" name="phno">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2" for="email">Email:</label>
                <div class="col-sm-4">
                    <input type="email" class="form-control" id="userMail" placeholder="Enter your mail" name="email">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2" for="pwd">Password:</label>
                <div class="col-sm-4">
                    <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-2">
                    <button type="submit" class="btn btn-default">Submit</button>
                    <button type="reset" class="btn btn-default" style="margin-left:25px">Reset</button>
                </div>
            </div>

        </form>
    </div>

</body>
</html>

错误消息:

Page not found (404)
Request Method: POST
Request URL:    http://127.0.0.1:8000/signup
Using the URLconf defined in heretohelp.urls, Django tried these URL patterns, in this order:

admin/
[name='orphanage_main_page']
signup.html [name='signup']
login.html [name='login']
The current path, signup, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

要求所有人在这种情况下为我提供帮助,我无法找到错误存在的地方或我做错了什么。 预先感谢。

0 个答案:

没有答案