2017-07-20 44 views
0

我在单个页面中有两个html模块用于注册,另一个用于登录。但是我找不到一种方法将这些数据保存在django数据库中。我是django的新手,所以我不知道如何将html文件与django关联起来。我把它作为头版连接起来,但就后端而言,我完全迷失了。我需要从头开始的帮助。这里是我的html文件。提前致谢。如何在django中保存html表单的数据

<body> 
 

 
<ul> 
 
    <li><a class="active" href="#home">Home</a></li> 
 
    <li><a href="#about">About Us</a></li> 
 
    <li><a href="#contact">Contact</a></li> 
 
</ul> 
 

 
<h1 style="color: white; text-align: center; padding: 260px 0px 0px 30px;">getTogether</h1> 
 
<h3 style="color: white; text-align: center;">A place to socialize and make friends</h3> 
 

 
<button onclick="document.getElementById('id01').style.display='block'" class="button button1">Sign Up</button> 
 

 
<div id="id01" class="w3-modal w3-animate-opacity"> 
 
    <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span> 
 
    <form class="modal-content animate" action="/home/user_profile" method="post"> 
 
    {% csrf_token %} 
 
    <div class="container"> 
 
     <label><b>Name</b></label> 
 
     <input type="text" placeholder="Enter Your Name" name="email" required> 
 

 
     <label><b>Email</b></label> 
 
     <input type="email" placeholder="Enter Email" name="email" required> 
 

 
     <label><b>Contact Number</b></label> 
 
     <input type="text" name="Phone Number" pattern="[7-9]{1}[0-9]{9}" placeholder="Enter Your Contact Number" name="email" required> 
 

 
     <label><b>Password</b></label> 
 
     <input type="password" placeholder="Enter Password" name="psw" required> 
 

 
     <label><b>Repeat Password</b></label> 
 
     <input type="password" placeholder="Repeat Password" name="psw-repeat" required> 
 
     <input type="checkbox" checked="checked"> Remember me 
 
     <p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p> 
 

 
     <div class="clearfix"> 
 
     <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button> 
 
     <a href="{% url 'user_profileview' %}"><button type="submit" class="signupbtn">Sign Up</button></a> 
 
     </div> 
 
    </div> 
 
    </form> 
 
</div> 
 

 
<script> 
 
// Get the modal 
 
var modal = document.getElementById('id01'); 
 

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target == modal) { 
 
     modal.style.display = "none"; 
 
    } 
 
} 
 
</script> 
 

 
<button onclick="document.getElementById('id02').style.display='block'" class="button button1">Sign In</button> 
 

 
<div id="id02" class="w3-modal w3-animate-opacity"> 
 
    <span onclick="document.getElementById('id02').style.display='none'" class="close" title="Close Modal">×</span> 
 
    <form class="modal-content1 animate" action="/home/user_profile" method="post"> 
 
    {% csrf_token %} 
 
    <div class="container"> 
 
     <label><b>Name</b></label> 
 
     <input type="text" placeholder="Enter Your Name" name="email" required> 
 

 
     <label><b>Password</b></label> 
 
     <input type="password" placeholder="Enter Password" name="psw" required> 
 

 
     <div class="clearfix"> 
 
     <button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn1">Cancel</button> 
 
     <a href="{% url 'user_profileview' %}"><button type="submit" class="signupbtn1">Sign In</button></a> 
 
     </div> 
 
    </div> 
 
    </form> 
 
</div> 
 

 
<script> 
 
// Get the modal 
 
var modal = document.getElementById('id02'); 
 

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target == modal) { 
 
     modal.style.display = "none"; 
 
    } 
 
} 
 
</script> 
 
</body>

+0

嗨,对于登录你应该有一个网址和注册你应该另一个URL例如=>登录网址是'/登录'和注册网址是'/注册'。在urls.py中找到这两个网址,然后在动作 – hadi

回答

0

由于sugested,你需要两个处理程序为每个表单。

  1. 在模板中,改变的形式的URL,一个/login/signup或什么的。

  2. 更改urls.py因此,例如:

    urlpatterns = [ 
        url(r'^/signup$', views.signup, name='signup'), 
        url(r'^/login$', views.login, name='login'), 
        # some other views 
    ] 
    
  3. views.py

    from django.contrib.auth import authenticate 
    from django.shortcuts import render 
    
    def signup(request): 
        if request.POST: # needed if you use the same URL for GET and POST 
         email = request.POST.get('email') 
         name = request.POST.get('name') 
         # and so on ... 
         # create user, set up password, etc. 
         user = User.objects.create_user(...) # your user database model 
         return render(request, 'yourapp/user_profile.html', { 
          'user': user 
         }) 
    
    def login(request): 
        if request.POST: 
         email = request.POST.get('email') 
         password = request.POST.get('psw') 
         # login user 
         user = authenticate(email=email, password=password) 
         if user: 
          return render(request, 'yourapp/user_profile.html', { 
           'user': user 
          }) 
         else: 
          # redirect to error page 
    

这是一个非常简单的设置,你将得到更好的使用Django的Form和班级观点。但是,如果你正在学习Django就没关系。

最后,请注意,在您的模板中,您正在使用<input name="email">作为多个元素。

请参考docs以更好地了解认证系统。你也可以阅读shortcuts

希望这会有所帮助。

+0

中使用它们。在我登录和注册后,用户最终会看到用户的个人资料页面,这就是为什么我给了同一个网址的原因。 。,我会改变他们,如果你说它是错误的 – Piyush

+0

我看到,那么在这两种情况下,你必须重定向到用户的个人资料页面。我会更新我的答案。 –

+0

而您的登录和注册网址的方法不是_wrong_,但最好将一个视图映射到一个url。 –