Python Django creation App

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.