Django note4

1、URL

1.1、how url works

1.1.1、recive a httprequest

1.1.2、find urls.py in the root URLconf

1.1.3、test the requested URL against each url entry.(every app can have a urls.py,but we always start with the root urls.py

1.2、def url(regex, view, kwargs=None, name=None):

1.3、url匹配處理是從上到下的,匹配後就會返回view,注意先後順序

1.3.1 If you want to design cool URLs for user profiles, the easiest solution to avoid URL collision is by adding a prefix like /u/vitorfs/, or like Medium does /@vitorfs/, where “@” is the prefix.

2、regex(正則表達式)

https://simpleisbetterthancomplex.com/references/2016/10/10/url-patterns.html

3、attribute PK

pk stands for Primary Key. It’s a shortcut for accessing a model’s primary key. All Django models have this attribute.

For the most cases, using the pk property is the same as id. That’s because if we don’t define a primary key for a model, Django will automatically create an AutoField named id, which will be its primary key.

If you defined a different primary key for a model, for example, let’s say the field email is your primary key. To access it you could either use obj.email or obj.pk.

4、testcase

5、Reusable Templates

5.1、create a base.html(模板) under templates/

5.2、leave{%block name%}{%endblock%} to be extends by other html

5.3、use home.html and topics.html to extends base.html,use {%block name%} {%endblock%} to fill in certain block

!use

{% extends 'base.html' %}

to claim the html you extend

example:

base.html

{% load static %}<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>{% block title %}Django Boards{% endblock %}</title>
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
  </head>
  <body>
    <div class="container">
      <ol class="breadcrumb my-4">
        {% block breadcrumb %}
        {% endblock %}
      </ol>
      {% block content %}
      {% endblock %}
    </div>
  </body>
</html>

home.html

{% extends 'base.html' %}
{% block breadcrumb %}
  <li class="breadcrumb-item active">Boards</li>
{% endblock %}

//sublime text 小技巧 ctrl+d批量選中進行修改//

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章