[Django] first try

Django

Create a project

$ django-admin startproject mysite
Project structure will look like this:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Run development server

$ python manage.py runserver 8080
Then open browser and enter localhost:8080

The development server will automatically reloads Python code change. But manual restart is still needed when adding a file or other rare situation.


Create a Polls app

Projects vs. apps
apps are web applications have specified purpose, project is a collections of apps.

$ python manage.py startapp polls

Project structure will look like this:

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

Add the first view

  1. Open polls/view.py and add code snippet below
from django.http import HttpResponse

# Create your views here.
def index(request):
    return HttpResponse("Hello world from polls")
  1. After modifying view file, we need let Django knows how to get the view file (map view to a URL)

Create polls/urls.py and add following code:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index')
]

Next step is modifying mysite/urls.py to let the whole project knows how to reach polls app.

  1. Import django.urls.include
  2. Include polls.urls

mysite/urls.py will look like this:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

The include() function allows referencing other URLconfs. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.


Knowledge

path()

path() function has four arguments: route, view, [kwargs], [name]

route:
order for URL matching
For example:

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

When localhost:8080/polls comes, Django will first match the first element in urlpatterns, which is polls/, matches!

When processing a request, Django starts at the first pattern in urlpatterns and makes its way down the list, comparing the requested URL against each pattern until it finds one that matches.

Writing your first Django app, part 1 | Django documentation | Django

HTTP verbs
Patterns don’t search GET and POST parameters, or the domain name.

For example:
https://www.example.com/myapp/
https://www.example.com/myapp/?page=3
Two urls are treated as same, Django will look for myapp/ in URLconf.

view:
When Django finds a matching url, it calls corresponding view with an HTTPRequest object

kwargs: (optional)
Arbitrary keyword arguments can be passed in a dictionary to the target view.

name: (optional)
Give the URL a name to avoid unambiguous naming routes


Connect database

We are using MySQL as our database.
In mysite/settings.py, find DATABASES variable, change default configure to:

    'default' : {
        'ENGINE' : 'django.db.backends.mysql',
        'NAME' : 'mysite',
        'USER' : 'root',
        'PASSWORD' : 'root',
    }

database mysite must exist in MySQL database before making setting change

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末然痊,一起剝皮案震驚了整個濱河市脏答,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機倘待,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來组贺,“玉大人凸舵,你說我怎么就攤上這事∈Ъ猓” “怎么了啊奄?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長掀潮。 經(jīng)常有香客問我菇夸,道長,這世上最難降的妖魔是什么仪吧? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任庄新,我火速辦了婚禮,結(jié)果婚禮上薯鼠,老公的妹妹穿的比我還像新娘择诈。我一直安慰自己,他們只是感情好出皇,可當(dāng)我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布吭从。 她就那樣靜靜地躺著,像睡著了一般恶迈。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上谱醇,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天暇仲,我揣著相機與錄音,去河邊找鬼副渴。 笑死奈附,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的煮剧。 我是一名探鬼主播斥滤,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼将鸵,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了佑颇?” 一聲冷哼從身側(cè)響起顶掉,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎挑胸,沒想到半個月后痒筒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡茬贵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年簿透,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片解藻。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡老充,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出螟左,到底是詐尸還是另有隱情啡浊,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布路狮,位于F島的核電站虫啥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏奄妨。R本人自食惡果不足惜涂籽,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望砸抛。 院中可真熱鬧评雌,春花似錦、人聲如沸直焙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽奔誓。三九已至斤吐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間厨喂,已是汗流浹背和措。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蜕煌,地道東北人派阱。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像斜纪,于是被迫代替她去往敵國和親贫母。 傳聞我的和親對象是個殘疾皇子文兑,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容