mysql django connection to complete simple login function


    
Log Function
    1. Access routes if not automatically redirected internal slash slash routes
    
    
    
all html files are written in the default templates folder

all the static files (css, js, front-end third-party libraries) are default folder on the static

mode html page introduction of external resources
    cdn
    local
    

static configuration file
STATIC_URL = '/ static /'
# static profiling
STATICFILES_DIRS = [
    the os.path.join (base_dir, 'static')
]
# can be exposed to the outside access server static folder below all the resources


STATIC_URL = '/ xxx /' # prefix with the interface name of your static folders had nothing to do
# the default folder name prefix as with the case of static files! ! !
# Configure static files
STATICFILES_DIRS = [
    os.path.join (base_dir, 'static'), # is your static folder path
    os.path.join (base_dir, 'static1'),
    os.path.join (base_dir, ' static2 ')
]
# PS: find words will be followed by a list of all of the static files to find the path to stop immediately return a 404 not found



form form submitting data to trigger actions in two ways
    <the INPUT of the type = "the Submit">
    <the Button> </ the Button>

How to address the submission of data form and manner specified?
    Action property controls the address submitted
    by:
        1. the full path
            <form action = "http://127.0.0.1: 8000 / Login / ">
        2. write only path suffix
            <form Action =" / Login / ">
        3. not write (submitted to the default current path)
    form the default form get request is
    
    
    different perform different logical way for the client request Code
    DEF Login (request):
        # acquisition request submitted by the UE mode
        print (request.method) # request method to get the whole string to uppercase
        IF request.method == 'the GET':
            return the render (request, 'Login .html ')
        elif request.method ==' POST ':
            return HttpResponse ( "brother received")
    
    Personal advice in accordance with the following written in this way reduce code redundancy and confusion structural problems
    Login DEF (Request):
        IF request.method == 'the POST':
            return the HttpResponse ( 'the OK')
        return the render (Request, 'the login.html')
    
    
    DEF Login (Request):
    # acquisition request submitted by the UE mode
    print ( request.method) # get request type is all uppercase string
    # IF request.method == 'the gET':
    # return the render (request, 'the login.html')
    # elif request.method == 'the POST':
    # return HttpResponse ( "received a brother")
    IF request.method == 'POST':
        Print (request.POST) # you Think of it as a large dictionary which kept all the data submitted by the client post
        # request.POST : <QueryDict: { 'username': [ 'Jason'], 'password': [ '123']}>
        Print (request.POST.get ( 'username ')) # value though a list but get value when it is to get the individual elements
        # Default value takes only the last element of which list
        # request.POST:<QueryDict: { 'username': [ 'Jason', 'Egon'], 'password': [ '123']}>
        Print (Request. POST.getlist ( 'username')) # To get a one-time value inside a list of all the data needed getlist ()
        # [ 'Jason', 'Egon']
        Print (request.POST [ 'password']) is not recommended # using this method to obtain data
        return HttpResponse ( 'the OK')
    return the render (Request, 'login.html')
    
    to obtain a list of value inside all the elements need to use getlist scenarios: the user's preference checkboxes
    get only get to the value of the list the last element
    
    
    
    Print ( `` request.GET``) # <QueryDict: { 'username': [ 'Jason'], 'password': [ '123']}>
    request.GET.get ( 'User')  
    # <QueryDict: { 'username': [ 'Jason ',' Egon '],' password ': [' 123 ']}>
    request.GET.getlist (' username ')
    
    
    
    Django database connection
install_as_MySQLdb () # tell django replaced by pymysql mysqldb database connection What is ORM?     Object-relational mapping         class "" "Table











    



        



    




        Object "" "table records
        the object's properties." "" The value of a field in a corresponding record
    

django orm is not automatically help you create a library, but can help you automatically create a table

Tip: a django project on the use of a library, not more than a django project uses a library






database migration (synchronous) command (******)
python3 manage.py makemigrations your database changes on a small notebook to record (and will not help you create a table)
python3 manage.py the migrate your database changes to the database are synchronizing





new data
# insert data operation database user table
# mode. 1:
USER_OBJ = models.User.objects.create (name = username, password = password)
# mode 2:
USER_OBJ = Models. the user (name = username, password = password)
user_obj.save () # call the save method to save objects to the database


query data
 
user_list = models.User.objects.all () # get all user data table
# QuerySet as long as you can point View query to get the current QuerySet object's internal sql statement
print (user_list.query)



Use a tag href href attribute specifies the path to the jump page can be written but it is recommended to write full path suffix to
<a href="/reg/" class="btn btn-success"> add data </a>
# Note path writing must slash

redirect may write someone else's web site can be your path to
return redirect ( '/ UserList')
return redirect ( '/ UserList /')



QuerySet object supports the index value is not recommended that you use the recommended use It comes .first () to help you get the first data


delete
models.User.objects.filter (id = 1) .delete ( ) # queryset will delete all data objects of all




query the data to note is that you get to in the end is a queryset or a data object
user_query = models.User.objects.filter (id = edit_id) .first () # If you are the first to get the data objects do not forget
the case of # filter when conditions do not exist will return an empty target queryset
<querySet that []> <class 'django.db.models.query.QuerySet'>

USER_OBJ = models.User.objects.get (id = edit_id) # can be directly used to get the data object itself but in the case of absence of the query given directly




editing
the editing target id manner of obtaining
Embodiment 1: input using a hidden tag
<input type = "hidden" name = "edit_id" value = "{{user_obj.pk}}">
mode 2:
<form Action = "? / Edit / edit_id user_obj.pk = {{}}" Method = "POST ">

Note: queryset modified target point Deletion of all data inside the target objects similar to batch operation
mode. 1:
models.User.objects.filter (ID = edit_id) .Update (name = username, password = password)
embodiment 2 : get the current data object
USER_OBJ = models.User.objects.filter (the above mentioned id = edit_id) .first ()
user_obj.name = username
user_obj.save ()



all data modifications inside the model layer associated with the table, as long as you modify it is necessary to re-run the database migration command
python manage.py makemigrations recorded on the books of small
python manage.py migrate real operational database

Guess you like

Origin www.cnblogs.com/tuanzibuku/p/10994409.html