我在一个页面中有两个html模式用于注册,另一个用于登录。但我无法找到在django数据库中保存该数据的方法。我是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 :(得分:0)
作为sugested,每个表单需要两个处理程序。
在模板中,更改表单的网址,一个geo = {
type: 'GeoChart',
options: {
width: '100%',
height: 300,
chartArea: { left: 10, top: 10, bottom: 0, height: "100%" },
colorAxis: { colors: ['#aec7e8', '#1f77b4'] },
displayMode: 'regions',
resolution : 'provinces'
}
}
和其他/login
或其他。
相应地更改/signup
,例如:
urls.py
在urlpatterns = [
url(r'^/signup$', views.signup, name='signup'),
url(r'^/login$', views.login, name='login'),
# some other views
]
:
views.py
这是一个非常简单的设置,您将更好地使用Django的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
表示多个元素。
请参阅docs以更好地了解身份验证系统。您可能还想了解shortcuts。
希望这有帮助。