程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Learning Django

編輯:Python

Simulate a user to access views First, we created one httprequest object And then put httprequest The object is passed to home_page View , Get is httpresponse return Extract corresponding content byte ,decode convert to html character string

tests.py

'''from django.test import TestCase # Create your tests here. class Smokeclass(TestCase): def test_bad_maths(self): self.assertEquals(1+1,3)'''''
from django.urls import resolve
from django.test import TestCase
from lists.views import home_page
from django.http import HttpRequest
class HomePageTest(TestCase):
def test_root_url_resolve_to_home_page_view(self):
found=resolve('/')
# resolve The function is django Functions used internally , For parsing url,
# And map it to the corresponding view function , When checking the site root path "/",
# Can I find home_page function 
self.assertEquals(found.func,home_page)
def test_home_page_returns_correct_html(self):
request=HttpRequest()
# establish httprequest object , When a user requests a web page in the browser 
# django What I see is httprequest object 
response=home_page(request)
# Pass on the object to home_page View 
html=response.content.decode('utf8')
# extract content, The result is the original byte , Send a user immediately 
# Browser's 0 and 1, Then call .decode(), Original byte 
# Convert it to the... Sent to the user html character string 
self.assertTrue(html.startswith('<html>'))
self.assertIn('<title>To-Do lists</title>',html)
self.assertTrue(html.endswith('</html>'))

views.py

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_page(request):
return HttpResponse('<html><title>To-Do lists</title></html>')
 Something to watch out for :
1)html.startswith and html.endswith Writing words
2) html=response.content.decode('utf8')
The encoding format is utf8
3) The request is HttpRequest, Return is HttpResponse

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved