Skip to content

Practice06 HTTP GET and POST

Englam edited this page May 22, 2017 · 1 revision

#HTTP GET and POST使用方法

#HTTP GET Example

在settings.py裡面增加templates folder,

TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)

在restaurants -> views.py ,裡面增加def welcome(request): (可參考原始碼)

將welcome.html放到 p_160 -> templates裡面

*測試方式:

http://127.0.0.1:8000/welcome/

http://127.0.0.1:8000/welcome/?user_name=

http://127.0.0.1:8000/welcome/?user_name=Taiwan




#餐廳選單 Example

http://127.0.0.1:8000/restaurants_list/

在urls.py加入url(r'^restaurants_list/', list_restaurants)

在Get Method 1下面選好選單,get method 已經把menu3的路徑選好了,所以選過去後,會把id一起帶到menu3

這邊也有增加評論的欄位,在views.py 可以找到def comment(request,id): (可參考原始碼)

*HTTP POST的用法在 comment的欄位 and comment.html可以看到



#Meta:

http://127.0.0.1:8000/meta/

views.py 可以找到def meta(request):(可參考原始碼)

它的用法是將所有資訊顯示出來



#Get Method 2

在views.py , 可以看到menu3的 http get用法1

def menu3(request):

if 'id' in request.GET and request.GET['id'] !='':

	restaurant = Restaurant.objects.get(id=request.GET['id'])

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

else:

	return HttpResponseRedirect("/restaurants_list/")



#Get Method 3

在views.py , 可以看到menu3的 http get用法2

def menu4(request,id):

if id:

	restaurant = Restaurant.objects.get(id=id)

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

else:

	return HttpResponseRedirect("/restaurants_list/")

Clone this wiki locally