??????????????????web?????????????????????????????????κ??????????к????????????????????н??????????????????????????web?????????????web??????????????????????????????????????????web?????????鷳???????????????????
?????????????????????????????????£??????? Django web??????requests????unittest????????????????????????
????Django????????????
?????????????Django?????????????????????????????Щ??????????web?????????????
???????????????
?????????????????????????????????????п?????????????????????У?????????????get??post???????????????????棬????????????????????
???????磬???????????????????????(test_get.py)
import requests
base_url = 'http://127.0.0.1:8000/polls'
r = requests.get(base_url)
code = r.status_code
text = r.text
print(code)
print(text)
???????????????
200
<html lang="zh-CN">
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Polls System</a>
</div>
</div>
</nav>
<br><br>
<div class="well">
<h3>Question List:</h3>
<ul>
<li><a href="/polls/1/">???????????????????</a></li>
<li><a href="/polls/2/">??????????????????????</a></li>
</ul>
</div>
<footer>
<p>&copy; Company 2016 & chongshi</p>
</footer>
</body>
</html>
?????????е?????÷????????????????????????????????????Json????????????????????.../polls/views.py?????и?????????????????????????????
fromdjango.shortcutsimportrender??get_object_or_404
fromdjango.httpimportHttpResponseRedirect
fromdjango.core.urlresolversimportreverse
from.modelsimportQuestion??Choice
fromdjango.httpimportHttpResponse
importjson
#Createyourviewshere.
#??????????
defindex(request):
latest_question_list=Question.objects.all()
dicts={}
iflatest_question_list:
forquestioninlatest_question_list:
dicts[question.id]=question.question_text
j=json.dumps(dicts)
returnHttpResponse(j)
else:
returnHttpResponse("questionlistnull")
#?????????????
defdetail(request??question_id):
choices=Choice.objects.filter(question_id=question_id)
dicts={}
print(question_id)
ifquestion_id:
forchoiceinchoices:
dicts[choice.id]=choice.choice_text
j=json.dumps(dicts)
returnHttpResponse(j)
.....
?????????????????????????????????????????????????????н??????????????????????????????????絥???????????????????????????question_id ?????????????????????????????????????????????????????????????????????????????????????????????Щ????????????????е?????????
??????????test_get.py?????
????200
????{"1": "u5341u4e00u56fdu5e86u4e03u5929u5047u671fu505au4ec0u4e48uff1f"?? "2": "u4f60u6700u60f3u5b66u7684u81eau52a8u5316u5de5u5177u662fu4ec0u4e48uff1f"}
????????ε??????json??????????????????????????????????unicode?????????????С???????????????????
??????Firefox???????Firebug??????л???“?????”?????

??????д??????
??????д????????????????????????????????д????????????????????????????????????????????get????post?????????????????Щ?????????ο??????????