Test views, templates, and URLs together with the Django test client

Source: Internet
Author: User

Django Test Client

The Self.client.get (URL) test client is a property name for the TestCase class in Django.

Now you can create a list in your Web site.

So, now it's time to test the new list in unit test.

Added a view function to the new manifest and mapped it to the URL ' ^/lists/new$ '. Where do you use it? Specify this URL in the action of the new List form on the home page.

The current implementation is:

1. Get home page, open the homepage. You can see the list of input backlog items.

2. Post home, you can create a list. and display the backlog in the list. Opens the URL of the new list

3. Post List page. To see new to-dos

The current problems are:

1. There is currently only one list

2. A list shows all the to-dos and cannot be quarantined.

Solve the problem:

To handle the isolation problem, you must modify the model and create a new List class. And let the class and item relate. Need to use model knowledge.

List is self-contained: use a regular in the URL, add a capturing group (. +), and the captured text is passed into the view as a parameter input

Item each item should exist in a list.

Capturing a group (. +) can not only capture/(. +)/can also capture/((. +)/new_item ..... So write the URL, to avoid the use of regular, matching is too wide.

Response = Self.client.get ('/lists/%id/'% (correct_list.id,))

Self.assertequal (response.context[' list '],correct_list) # Response.context represents the context to pass in the render function. The Django client has the context attached to the response object for easy testing.

In Template:

{% for new_item_text in list.item_set.all%} #. Item_set is called a reverse query. is a very useful ORM feature provided by Django. You can query related records for an object in other tables.

So in the from of the template. You can pass in a modle instance. Then find the relevant other object instances based on this instance:

<form method= "POST" action= '/lists/{{list. id}}/new ' >
<input name= "Item_text" id= "Id_new_item" placeholder= "Enter a to-do item"/>
{% Csrf_token%}
</form>
<table id= "Id_list_table" >
{% for new_item_text in list.item_set.all %}
<tr><td>{{Forloop.counter}}: {{new_item_text.text}}</td></tr>
{% ENDFOR%}
</table>

Test views, templates, and URLs together with the Django test client

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.