django user login design of novice Python

Keywords: Django Python Database Pycharm

Environmental preparation

Front end framework semantic ui
Python 3.6.4
pip install django
The development tool is pyCharm

Data model design

The user table contains the following information

  • User name
  • Full name
  • Gender
  • Password
  • Cell-phone number
  • Creation time
  • Update time
  • Is it effective?

The user/models.py user model is as follows

user is the name of the created app

from django.db import models

# Create your models here.


class User(models.Model):
    gender = (('1', 'male'), ('2', 'female'))
    account = models.CharField(max_length=200)
    name = models.CharField(max_length=200)
    password = models.CharField(max_length=200)
    sex = models.CharField(max_length=25, choices=gender, default='')
    email = models.EmailField(max_length=200)
    phone = models.CharField(max_length=55)
    creDate = models.DateTimeField(auto_now_add=True)
    lastLogin = models.DateTimeField(auto_now=True)
    enable = models.IntegerField(default=1)

    class Meta:
        db_table = 'User'
        verbose_name = 'user'
        verbose_name_plural = 'user'

    def __str__(self):
        return self.name

Meta (metadata)

  • DB table is used to specify a custom database table name
  • Verbose [name] gives the model class a more readable name
  • Verbose? Name? Plural specifies what the plural form of a model is generally the same as verbose? Name Use str to display object information

Generate database tables

Enter the terminal terminal of Pycharm and execute the command

python manage.py makemigrations

After the execution is successful, Django automatically creates 0001 "initial.py file for us, saves our first migration data, and then executes the command

python manage.py migrate

View that the corresponding user table has been created in the database

In the future, if the user model changes, just execute two commands here

Routing settings

In order to facilitate code management, the level 2 routing setting is used. First, configure urls in the project as follows

path(r'user/', include('user.urls')),

Then configure the corresponding urls of user application as follows:

url(r'login/', views.login, name='user'),

View preliminary settings

from django.shortcuts import render, redirect
from user import models

def index(request):
    pass
    return render(request, 'user/login.html', locals())

HTML page file

The HTML page file uses the following code for the demo part of the login interface of the semantic ui official website

<form id='login-form'  class="ui large form error"  action="/user/login/" method="POST">
        <div class="ui stacked segment">
        <div class="field">
          <div class="ui left icon input">
            <i class="user icon"></i>
            <input type="text" name="email" placeholder="Input mailbox">
          </div>
        </div>
        <div class="field">
          <div class="ui left icon input">
            <i class="lock icon"></i>
            <input type="password" name="password" placeholder="Input password">
          </div>
        </div>

        <div class="ui fluid large teal submit button">Land</div>
      </div>

      <div class="ui error message">
      </div>

    </form>

Posted by plouka on Wed, 01 Apr 2020 00:13:14 -0700