Web???????????
???????????? ???????[ 2016/3/1 11:32:05 ] ??????????????? ??????
??????????????????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>© 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?????????????????Щ?????????ο??????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11