Django 【進階篇】

Django 流程html

Django url經常使用匹配語法python

 

幾種匹配(在與項目同名的文件夾裏的urls.py裏寫,對應的函數在APP*裏的views.py裏寫)mysql

 

from django.conf.urls import url
 
from . import views
 
urlpatterns = [
    url(r'^articles/2003/$', views.special_case_2003),#匹配文章的年是2003的
    url(r'^articles/([0-9]{4})/$', views.year_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]

 

Name Groupsweb

from django.conf.urls import url
 
from . import views
 
urlpatterns = [
    url(r'^articles/2003/$', views.special_case_2003),
    url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
    url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
    url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail),
]

上面url裏面的匹配和正則表達式是同樣的,根據咱們本身的須要去選擇相應的正則表達式正則表達式

include在url中的用途:項目裏面的每一個應用即app*裏面都有本身的urlsql

注意url裏面不能寫動詞,雖然不出錯,可是不規範數據庫

 

from django.conf.urls import include, url
 
urlpatterns = [
    # ... snip ...
    url(r'^community/', include('django_website.aggregator.urls')),
    url(r'^contact/', include('django_website.contact.urls')),
    # ... snip ...
]

 

url後面加上了字典,它的用途是:django

若是在全局的url中加上字典的話,那麼在每個視圖裏都會加上該字典裏的類型做爲參數瀏覽器

注意:是用戶對象,而不是用戶名,這裏是對一堆的信息封裝成爲對象app

GETPOST請求方法:

GETPOST請求方法:

GET請求的東西是明文顯示的   POST請求的東西是非明文的  PostMan插件(只能用於chrone瀏覽器)

Django Template渲染:

字符串 字典 列表,

>>> from django.template import Context, Template
>>> t = Template('My name is {{ name }}.')
>>> c = Context({'name': 'Stephane'})
>>> t.render(c)
u'My name is Stephane.'
>>> from django.template import Template, Context
>>> person = {'name': 'Sally', 'age': '43'}
>>> t = Template('{{ person.name }} is {{ person.age }} years old.')
>>> c = Context({'person': person})
>>> t.render(c)
u'Sally is 43 years old.'
>>> from django.template import Template, Context
>>> t = Template('Item 2 is {{ items.2 }}.')
>>> c = Context({'items': ['apples', 'bananas', 'carrots']})
>>> t.render(c)
u'Item 2 is carrots.'

Django Template動態顯示數據:

view.py寫好多的數據,要顯示在網頁上,那麼就要在template/app01/index.html中循環的語句

 

<ul>
    {%for user_obj in user_objs%}
        {% ifequal user_obj.username 'alex'%} 
            <li  style="background-color:red">username:{{user_obj.username}},name:{{user_obj.name}}</li>
        {%else%}
            <li style="">username:{{user_obj.username}},name:{{%user_obj.name%]}</li>
        {%endifequal%}
    {%endfor%}
</ul>

 

<ul>
    {%for user_obj in user_objs%}
        {% if forloop.counter divisibleby:"2"%} 
            <li  style="background-color:red">username:{{user_obj.username}},name:{{user_obj.name}}</li>
        {%else%}
            <li style="">username:{{user_obj.username}},name:{{%user_obj.name%]}</li>
        {%endifequal%}
        {%endfor%}
</ul>

Django Template經常使用語法:

一、先作的子頁面(views.py)

def page1(request):
      return render(request,'app01/page1.html')

二、繼承(page1.html)

{% extends 'app01/index.html' %}
{% block header-menu %}
     <h1>sub menu 1....3....45...5</h1>
{% endblock %}
{% block content-container %}
page1
{% endblock %}

三、index.py

<body>
{% block header-menu %}
<ul>
       <li style="display:inline-block">Home<li>
       <li style="display:inline-block">page1<li>
       <li style="display:inline-block">page2<li>
       <li style="display:inline-block">page3<li>
</ul>
{% endblock %}

Django Template 模板繼承和複用:

首先看一下項目中包含的文件吧!

register_com.html:

<div style="background-color:aquamarine">
    <form  action="/register">
         Username:<input name="username" type="text">
         Password:<input name="password" type="password">
    </form>
</div>

page1.html:加入的語句爲:

 

{% block father-container %}
   {% include 'app01/register_com.html' %}
{% endblock %}

 

page1.html:

{% extends 'app01/index.html' %}
{% block header-menu %}
     <h1>sub menu 1...3..45..5</h1>
{% endblock %}
{% block content-container %}
     <h1>page</h1>
     {% block father-container %}
           {% include 'app01/register_com.html' %}
      {% endblock %}
{% endblock %}

Django配置使用數據庫:

要配置好數據庫的鏈接(在settings.py中)

須要安裝mysql

在建立數據庫的時候爲了支持中文,須要這樣寫:create database ** charset utf8;(**爲本身起的數據庫的名字)

 

Django ORM經常使用語法:

 

APP中有一個models.py,在這裏建立數據庫

 

settings.py裏:

 

1,把本身的應用添加進去,

 

2,建立數據庫,

 

3,在admin.py中引入models

 

4,同步數據庫,

 

>>>python manage.py makemigrations
>>>python manage.py migrate

 

5,建立超級用戶,

 

>>>python manage.py createsuperuser

 

6,最後在models.py中不要忘了添加如下的語句,在python 3.0str不用unicode

 

Django ORM經常使用字段:

字段:

blank:若是它爲true,則其對應的input能夠爲空

editable:若是它爲false,則其對應的input在頁面上就不顯示了,可是在數據庫中仍是存在的

help_text:提示對應的input填什麼內容

verbose_name:對應的input的名字

 

Django Admin建立數據庫數據:

 

查找和修改:

建立:2種方式,

 

過濾:

Django 增刪改查:

模糊查找

 

改:

關聯查詢:

相關文章
相關標籤/搜索