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

Django architecture + unit test form

編輯:Python

Things but three , Copying three times requires refactoring

1)

assertTemplateUsed yes Django TestCase Class , Used to check which template the response is rendered with ( Be careful , This method can only test the response obtained through the test client )

2)

dict.get(key, default=None)

Parameters

key – The key to look up in the dictionary .
default – If the value of the specified key does not exist , Returns the default value .

Return value

Returns the value of the specified key , If the key is not in the dictionary, return the default value None Or set the default value .

3) Cross-site request forgery csrf

https://blog.csdn.net/weixin_40482816/article/details/114301717

4) Input and

input label Correct setting is required name attribute ,name=“item_text”, If you don't have this property , The user's input cannot be compared with request.POST Correct correlation

home.html

<html>
<title>To-Do lists</title>
<body><form method="post">
<input name="item_text" id="id_new_item" placeholder="enter a todo item"/>
{
% csrf_token %}
<table id="id_item_table">
<tr><td>{
{
 new_item_text }}</td></tr>
</table>
</form></body>
</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>')'''
from django.shortcuts import render
'''def home_page(request): if request.method=='POST': return HttpResponse(request.POST['item_text']) return render(request,'home.html')'''
def home_page(request):
return render(request, 'home.html',{
'new_item_text':request.POST.get('item_text',''),})

urls.py

"""superlists URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.10/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """
from django.conf.urls import url
from django.contrib import admin
from lists import views
from django.conf.urls import url
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^$',views.home_page,name='home'),
]

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>'))
def test_home_page_returns_correct_html_chonggou(self):
response=self.client.get('/')
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>'))
self.assertTemplateUsed(response,'home.html')
def test_user_home_template(self):
response=self.client.get('/')
self.assertTemplateUsed(response,'home.html')
def test_can_save_a_POST_request(self):
response=self.client.post('/',data={
'item_text':'a new list item'})
self.assertIn('a new list item',response.content.decode())
self.assertTemplateUsed(response, 'home.html')

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