Enter the following command
Python manage.py Startapp apitest
Add an app to the Autotest Project project
Add "Apitest" to the settings.pyo, such as
Create a View
Add the test function to the Apitest/views
from Import Render from Import HttpResponse # Create your views here. def Test (Request): return HttpResponse ('Hello test')
Creating mappings
Now map the functions in the view to the browser front page and add the following to the autotest/urls.py:
fromDjango.contribImportAdmin fromDjango.urlsImportPath fromApitestImportViews#import views, note the project path, open to open from the Autotest directory. Urlpatterns=[Path ('admin/', Admin.site.urls), Path ('test/', Views.test),#adding paths and functions]
Start the service:
Python manage.py runserver
Browser access: http://127.0.0.1:8000/test/
Creating templates
1. Create the Templates folder under Apitest and create the login.html file under the folder.
and add the following content:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <Metahttp-equiv= "Content-type"content= "test/html; charset=utf-8"> <title>Login</title></Head><Body><H1>Login</H1><formMethod= "POST"Action= "login/">{% Csrf_token%}<BR> <inputname= "username"type= "text"placeholder= "username"> <BR> <inputname= "Password"type= "Password"placeholder= "Password">{{Error}}<BR> <BR> <ButtonID= "Submit"type= "Submit">Submit</Button> </form></Body></HTML>
2. Creating Mappings in autotest/urls.py
fromDjango.contribImportAdmin fromDjango.urlsImportPath fromApitestImportViews#import views, note the project path, open to open from the Autotest directory. Urlpatterns=[Path ('admin/', Admin.site.urls), Path ('test/', Views.test),#adding paths and mapping functionsPath'login/', Views.login),#This path is added here and is mapped to the login function in views when accessing login/]
3. Create the login function in apitest/views.py
fromDjango.shortcutsImportRender fromDjango.httpImportHttpResponse#Create your views here.defTest (Request):returnHttpResponse ('Hello Test')defLogin (Request):returnRender (Request,'login.html')#This returns the content of the specified page using render return.
4, visit the login page: http://127.0.0.1:8000/login/page shows as follows:
Python Django creation App