Python development and Interface Test Learning notes

Source: Internet
Author: User

This is the learning notes that I have accumulated in my study with the worm, and it is easy for me to learn from the beginner of Python development and interface testing.

I. Development of voting systems

1, refer to the official website document, create voting system.

https://docs.djangoproject.com/en/1.11/intro/tutorial01/

Before you create a voting system, make sure that the Django and official documents you have installed are consistent.

====================

Win 10

Python 3.6.2

Django 1.11.3

Pycharm compiler

====================

2. Create the project Pollsweb and create the polls app

Open a polls/setting.py file to add:

Polls is the app we just created, and bootstrap3 is our additional download front-end framework

Final directory structure:

3. Create a model

General Web Development First design database, database design, project completed more than half, the importance of visible database.

Open polls/models.py to write as follows:

Perform database table generation and synchronization

4. Admin Management

Django provides a powerful background management, for Web applications, the background is essential, such as the current voting system, how to add problem and problem options? Directly manipulate the database add, obviously troublesome, inconvenient, and unsafe. So, the management of the background can do this kind of work.

Open the polls/admin.py file and write the following:

The purpose of the current script is to put the Model (database table) in admin background and run the Web container:

First, we'll create a Super Admin user,

Once you've created the Superuser, you're ready to run our container:

Login to our backstage: 127.0.0.1:8000/admin

Enter the login name and password that we just created,

To add a question, click on the Add button:

Click Save

5. Writing a View

The view plays an important role, before it refers to the front page, and then the background database. Queries the contents of a database table to appear on the page.

To write a polls/views.py file:

6. Configure URLs

A URL is a request configuration file that is determined by which function is processed by the request in the page.

First configure polls/urls.py, this file needs to be created by itself, create a urls.py file under polls:

After writing, edit the total URLs. Open pollweb/urls.py File:

7. Create a template

A template is a previous page that is used to display data on a Web page.

Create the polls/templates/polls/directory first, and create the index.html, detail.html, and result.html files in this directory, respectively

Index.html

Result.html

Detail.html

8. system function Display

http://127.0.0.1:8000/polls/now we can try to visit our page:

Ii. development of voting system interface

Although the function of the voting system has been developed, we have not developed a dedicated interface, and in the current polling system, when we call a GET or POST request, the system returns the entire page and returns the test along with the page

2.1. Transform the voting system interface

For example, when we want to invoke the interface of all problems (test_get.py)

Run the program and get the following results:

The specific interface should return data instead of the entire page, while the data is generally formatted as an array, dictionary, or JSON format

Therefore, you need to view the layer (.. /polls/views.py) to transform it to provide an interface and simply return the data

We first on the front two interface transformation, in fact, there are many modifications here are not complete, such as a single problem of all the options interface, accept the parameters question_id if empty, should prompt the parameter error, if the query is not related to the problem, you should prompt the query result is empty, if the type is not a number, You should be prompted for a type error.

Execute the test_get.py file again:

This time, we get JSON-type data. However, the return value encodes the Chinese Unicode. Here is a little trick to convert it into Chinese.

http://127.0.0.1:8000/polls/opens in Google Chrome, we'll see those return values above

Click on the keyboard F12, you can open the console, according to the above-circled click, you can see the Chinese

2.2. Writing Interface Documents

Writing an interface document is also a very important part, because the interface we write needs to be called to others, so how do others know if our interface is called with Get or post? What are the parameters? It's time to refer to the interface documentation.

1. Get all questions

Url

http://127.0.0.1:8000/polls/

Request type

Get

Required Parameters

No

return format

Json

return results

{1: "What to do in National Day", 2: "What to do in the Mid-Autumn Festival", 3: "What to Do in Summer", 4: "What To Do"}

Type of error

Not currently

2. Get all the options for a single question

wr.

http://127.0.0.1:8000/polls/

Request type

Get

Required Parameters

question_id

return format

Json

return results

{1: "At Home", 2: "Work Internship", 3: "Internet"}

Type of error

Not currently

Probably the interface document is so written, for this document, it is convenient how we do the interface test

3, for writing interface testing, we will involve two technologies. One is UnitTest unit Test framework and request Library

The code for the interface is done, and finally we look at the results returned:

Here, our system development and interface testing has basically been completed. This is also the note that I get with the bug Master Tutorial.

Python development and Interface Test Learning notes

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.