Django---简单的登陆界面

一、创建项目成功后用sublime_text打开项目

    1、views.py文件:   

from django.shortcuts import render
from app1.models import user
# Create your views here.
def gotoRegister(request):
return render(request,'login.html')
def addDatas(request):
UserName=request.GET.get('UserName')
UserPasswd=request.GET.get('UserPasswd')
UserAge=request.GET.get('UserAge')
if user(UserName=UserName) and user(UserPasswd=UserPasswd) and user(UserAge=UserAge):
user(UserName=UserName,UserPasswd=UserPasswd,UserAge=UserAge).save()
return render(request,'login.html',{'msg':'注册成功!','UserName':UserName,'UserPasswd':UserPasswd,'UserAge':UserAge})
else:
return render(request,'login.html',{'msg':'信息未输入'})
def show(request):
userAll=user.objects.all()
print(userAll)

return render(request,'show.html',{'msg':'数据展示成功','userAll':userAll})

    2、models.py文件:(配置生成数据库)

class user(models.Model):
UserName=models.CharField('UserName',primary_key=True)
UserPasswd=models.CharField('UserPasswd',max_length=20)
UserAge=models.CharField('UserAge',max_length=30)
pass

    3、settings.py文件:(配置数据库连接,使用sqlite3数据库)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }

}

    4、settings.py文件:(配置数据库连接,使用sqlite3数据库)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }

}

5、urls.py 文件:

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from app1 import views as app


urlpatterns = [
    url('admin/', admin.site.urls),
    url('^$', app.gotoRegister),
    url('addDatas',app.addDatas),
    url('show',app.show),

]

6、login.html 文件:

        

Register








{{UserName}}{{msg}}




7、show.html文件:

显示用户信息







{% for i in userAll %}





{% endfor %}
用户名密码年龄
{{i.UserName}}{{i.UserPasswd}}{{i.UserAge}}

{{msg}}


你可能感兴趣的:(Python---Django)