Skip to content

Practice03 Menu Page with APP

Englam edited this page May 22, 2017 · 1 revision

#App的使用方法,用Menu.html當範例



#開啟project

django-admin.py startproject my_menu_app



#run http server

cd /Documents/python/Django/practices/my_menu_app

or

cd my_menu_app/

python manage.py runserver

url = http://127.0.0.1:8000/



#Create a App

建立App,區分服務的使用

example: python manage.py startapp restaurents



#App裡面的templates

建立templates folder在restaurant, 不需要在settings加templates加路徑

在restaurants -> templates, 放入menu and menu2.html



#設定settings.py, 支援建立起來的App

要把INSTALLED_APPS 加入 restaurents ,因為它是一個app



#urls.py

要變成這樣

url(r'^menu/', menu),

url(r'^menu2/', menu2),



#views.py

在restaurants底下增加一個 views.py

加入下面的程式碼:

from django.shortcuts import render_to_response

def menu(request):

food = {'name' : '1111', 'price' : 60, 'comment' : 'Delicious', 'is_spice': False}

return render_to_response('menu.html',locals())

def menu2(request):

food1 = {'name' : '蕃茄炒蛋', 'price' : 60, 'comment' : 'Delicious', 'is_spice': False}

food2 = {'name' : '蒜泥白肉', 'price' : 160, 'comment' : '人氣推薦', 'is_spice': True}

foods = [food1, food2]

return render_to_response('menu2.html',locals())



#確認model裡的語法是否有錯

python manage.py XXXX

example : python manage.py check

用來確認model裡的語法是否有錯



#確認migration

python manage.py makemigrations XXXX

example : python manage.py makemigrations restaurants

會確認migration,如果模組有異則產生新的migration,並產生模組

在migration folder裡面可以看到第一次 model as 0001_initial.py

dependencies ==根據XXX 做了異動

operations ==實際模組的運行



# Modules to SQL language

python manage.py sqlmigrate restaurants XXXX

example :

python manage.py sqlmigrate restaurants 0001

python manage.py sqlmigrate restaurants 0002

Clone this wiki locally