如何使用django框架为网站创建登录和注册页面?

时间:2017-01-05 09:20:36

标签: python html django postgresql

我是Django的新手。我无法将用户输入值存储到postgres DB中。我使用Models.py文件创建了一个表,并使用模板文件创建了一个用户界面。如何使用view.py文件将vaues传递给数据库。有人帮我plsz

2 个答案:

答案 0 :(得分:1)

简单登录 在users / views.py

from django.shortcuts import render
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, logout,login
from django.http import HttpResponse, HttpResponseRedirect

def user_login(request):
    if request.method == "POST":
        phone = request.POST.get('phone')
        password = request.POST.get('password')
        user = authenticate(username=phone, password=password)
        if user:
            login(request,user)
            return HttpResponseRedirect('/users/home')
        else:
            error = " Sorry! Phone Number and Password didn't match, Please try again ! "
            return render(request, 'login/index.html',{'error':error})
    else:
        return render(request, 'login/index.html')

并在模板login / index.html

<html>
<body>
{% if error %}
{{ error }}
{% endif %}
<form method="post" action="/users/login/">{% csrf_token %}
<input type=text placeholder="PhoneNo" name="phone">
<input type=password placeholder="Password" name="password">
<input type="submit" value="login">
</body>
</html>

进行注册

登录/ signup.html

<html>
<body>
<form method=post action="users/signup/">{% csrf_token %}
<input type="text" name="phone" placeholde="Phone No">
<input type="text" name="email" placeholde="Email">
<input type="text" name="password1" placeholde="Password">
<input type="text" name="password2" placeholde="Password Again">
<input type="submit" value="signup">
</form> 
</body>
</html>

用户/ views.py

def users_signup(request):
    if request.method == 'POST':
        email = request.POST.get('email')
        phone = request.POST.get('phone')
        pass_1 = request.POST.get('password1')
        pass_2 = request.POST.get('password2')
        if pass_1 == pass_2:
             user = User.objects.create_user(
                                              username=phone,
                                              email=email,
                                              password=pass_1,
                                             )
             return HttpResponseRedirect("/")
        else:
             error = " Password Mismatch "
             return render(request, 'login/signup.html',{"error":error})
    else:
         return render(request, 'login/signup.html')

主项目文件夹中的主urls.py,其中有settings.py文件将是

from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = [
         url(r'^admin/', admin.site.urls),
         url(r'^users/', include('users.urls')),
]

app的url.py也说'#34;用户&#34;

from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
       url(r'^login/', "users.views.user_login", name='login_url'),
       url(r'^signup/', "users.views.user_signup", name='signup_url'),
)

答案 1 :(得分:0)

假设您的UI基于表单,您在view.py中需要做的就是处理在提交此表单时从客户端发送的POST请求。因此,您定义了一个方法(比如signup),如果需要,该方法将传递一个请求以及可能的其他参数。在其中,您需要进行必要的验证(即检查此用户是否已存在)并通过render()返回带有错误消息的新页面,或者如果一切正常则返回到下一页。

官方tutorial中的更多细节,正如@anuragal正确指出的那样非常好