Set up a book list page, display book name lists, and achieve click on the title to jump to the book details page, display the book details.
- Introduction to URL methods
- Function: Returns a reference to an absolute path (a URL that does not contain a domain name); The reference matches a given view function and
Some optional parameters.
- Grammar:
{% url ‘some-url-name‘ value1 value2 %}
- The parameter ' Some-url-name ' indicates the routing address in the urls.py file;
- Parameters value1 and value2 indicate the value of stitching, optional.
- For example, urls.py:
url(r‘^bookinfo/(\d+)/$‘, polls_views.bookinfo, name=‘book‘)
In the HTML code: {% url ‘book‘ 3 %}
;
After stitching the return address is: bookinfo/3/
Existing databases and information |
information stored in database Djangodemo:
Table Polls_book
+----+--------------+-----------+
| ID | name | person_id |
+----+--------------+-----------+
| 1 | Siege | 1 |
| 2 | Butterfly Dream | 2 |
| 3 | Robinson Crusoe | 3 |
| 4 | The Little Prince | 4 |
+----+--------------+-----------+
Table Polls_person
+----+------+-----+
| ID | name | Age |
+----+------+-----+
| 1 | Joe | 12 |
| 2 | Walt | 18 |
| 3 | Walt | 17 |
| 4 | Jany | 20 |
| 5 | John | 29 |
+----+------+-----+
- First write the book list page
- Automatic hyperlink stitching for hyperlinks
- Write a book details page
- Project directory information
- Establish page Routing
Add in the urls.py file
# 导入路由,支持正则表达式fromimport url# 在路由匹配模式中添加图书列表页面的路由= [ url(r‘^booklist/$‘# 定义拼接地址,获取书籍信息 url(r‘^bookinfo/(\d+)/$‘, polls_views.bookinfo, name=‘bookinfo‘)]
- 1. Add in the views.py file
# 图书列表页面控制器def booklist(request): # 导入图书类 fromimport Book # 实例化一个图书对象 = Book.objects.all() # 建立空字典存储booklist = {} dict_book[‘booklist‘= books # 向bookList.html页面传入数据dict_book return‘bookList.html‘, dict_book)
- 2. Create a new booklist.html file under the Templates folder and add
{# 在bookList.html文件的body下添加如下代码 #}<body> 图书架 <ul> {% for book in booklist %} {# 使用每本书的book.id作为获取详情的查询条件,生成链接 #} <li><a href="{% url ‘bookinfo‘ book.id %}">{{ book.name }}</a></li> {% endfor %} </ul></body>
- 3. Define control methods for obtaining detailed information about books in the view.py file
# 获取书籍信息def bookinfo(request, id): # 导入图书类 from polls.models import Book # 实例化一个图书对象,使用book.id查询该书籍数据 book = Book.objects.get(id=id) # 建立空字典存储booklist dict_book = {} # 存储book书名 dict_book[‘book‘] = book.name # 存储book作者 dict_book[‘author‘] = book.person.name # 存储book作者年龄 dict_book[‘author_age‘] = book.person.age # 向bookInfo.html页面传入数据dict_book return render(request, ‘bookInfo.html‘, dict_book)
- 4. Create a new bookinfo.html file under the Templates folder and add
{# 在bookInfo.html文件的body下添加如下代码 #}<body> {{ book }} <ul> <li>{{ author }}</li> <li>年龄:{{ author_age }}</li> </ul></body>
- Accessing http://127.0.0.1:8000/booklist/in the browser
- Click on "Robinson Crusoe
As can be seen, the address bar in the 127.0.0.1:8000/BOOKINFO/3 "3" is based on the book "Robinson Crusoe" ID obtained, "Robinson Crusoe" in the database table polls_book the corresponding ID is 3.
Table Polls_book
+----+--------------+-----------+
| ID | name | person_id |
+----+--------------+-----------+
| 1 | Siege | 1 |
| 2 | Butterfly Dream | 2 |
| 3 | Robinson Crusoe | 3 |
| 4 | The Little Prince | 4 |
+----+--------------+-----------+
- The above job condition is that you have completed the normal configuration of Django, and normally open the server;
- The data in the database is pre-added, and this is just querying the data in the database.
- Capacity is limited, and the correction is welcome.
Using the URL method in Django to realize the automatic generation of hyperlink address for address dynamic stitching