Landing function landing success page (5)

Posted by hypertech on Thu, 02 Apr 2020 06:31:54 +0200

"login success!" will be returned after the last article is successfully logged in String is just a temporary solution, just for the convenience of verifying the processing logic of login. After verification, it needs to be replaced by html page.

1. Think about what will happen after the successful login? The press conference management page should be displayed. Therefore, first create the.. / templates/event_manage.html page.

<!DOCTYPE html>
<html>
<head>
    <title>Event Manage Page</title>
</head>
<body>
    <h1>Login Success!</h1>
</body>
</html>

2. Open the file.. / FirstApp/views.py. The changes are as follows.

from django.shortcuts import render

from django.http import HttpResponse
,HttpResponseRedirect


# Create your own view
def index(request):
    return render(request, "index.html")

# Landing method

def  login_action(request):

    if request.method == 'POST':

        username = request.POST.get('username', '')

        password = request.POST.get('password', '')

        if username == 'admin' and password == 'admin123':

            return HttpResponseRedirect('/event_manage/')

            pass

        else:

            return render(request, 'index.html', {'error':'username or password error!!!'})

        pass

    pass



# Press conference management

def event_manage(request):

    return render(request, "event_manage.html")

Here, a new HttpResponseRedirect class is used, which can redirect the path, so as to point the request after the successful login to the / event ﹐ manage / directory, i.e http://127.0.0.1:8000/event_manage/ . Create the event manage function to return to the event manage.html page.

3. Finally, don't forget to add the route of event manage / to the.. / FirstProject/urls.py file.

from django.conf.urls import url
from django.contrib import admin
from FirstApp import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^admin/', admin.site.urls),
    url(r'^index/$', views.index),
    url(r'^login_action/$', views.login_action),
    url(r'^event_manage/$', views.event_manage),
]

4. View the operation results after the successful login.

Topics: Django