本文整理汇总了Python中django.shortcuts.HttpResponse.write方法的典型用法代码示例。如果您正苦于以下问题:Python HttpResponse.write方法的具体用法?Python HttpResponse.write怎么用?Python HttpResponse.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.shortcuts.HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def register(request):
"""
注册处理事件
:param request:注册请求
:return:
①result: '0'表示成功, '1'表示用户名重名, '2'表示未知错误,可能昵称/邮箱/密码为空,可能传输出错
②
"""
# 回复
response = HttpResponse()
response['Content-Type'] = 'application/json'
if request.method == 'POST':
# 获取客户端信息
nickname = request.POST.get('userName', '')
password = request.POST.get('userCode', '')
avatar = request.POST.get('avatar', '')
if nickname and password:
try:
user = User.objects.get(nickname=nickname)
result = '1'
except ObjectDoesNotExist:
user = User(nickname=nickname, mail='nomail', password=password, create_time=time.time(),
action_time=time.time())
user.save()
result = '0'
else:
result = '2'
else:
result = '-1'
response.write(result)
response.write(fetch_recommend()) # 返回首页信息
return response
示例2: login
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def login(request):
"""
用户登陆事件
:param request:登陆请求
:return:
①result: 返回码, '0'表示成功 '1'表示用户不存在 '2'表示用户密码错误 '3'表示未知错误
②info:dict数据,登陆成功返回的用户信息就保存在这个json里面了
"""
response = HttpResponse()
response['Content-Type'] = 'application/json'
result = '3'
if request.method == 'POST':
# 获取客户端信息
nickname = request.POST.get('userName', '')
password = request.POST.get('password', '')
if nickname and password:
try:
# 用户存在
user = User.objects.get(nickname=nickname)
if user.password == password:
# 用户密码正确
result = '0'
else:
result = '-1'
except ObjectDoesNotExist:
result = '-1'
response.write(result)
response.write(fetch_recommend())
return response
示例3: save_workspace
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def save_workspace(request):
data = request.POST["my_data"]
response = HttpResponse(mimetype="text/plain")
response.write(data)
date = datetime.datetime.now().strftime("%Y-%m-%d-T-%H-%M-%S")
response["Content-Disposition"] = "attachment; filename=%s-%s.cdOnline" % ("workspace", date)
return response
示例4: delivery_csv
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def delivery_csv(self, request, queryset):
# PREPARE
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=deliveries.csv'
writer = csv.writer(response, csv.excel)
# PROCESS
response.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
writer.writerow([
smart_str(u"ID"),
smart_str(u"MESSAGE ID"),
smart_str(u"TIMESTAMP"),
smart_str(u"MAIL FROM"),
smart_str(u"MAIL TO"),
smart_str(u"SMTP RESPONSE"),
smart_str(u"REPORTIN MTA")])
for obj in queryset:
writer.writerow([
smart_str(obj.pk or ''),
smart_str(obj.sns_messageid or ''),
smart_str(obj.mail_timestamp or ''),
smart_str(obj.mail_from or ''),
smart_str(obj.address or ''),
smart_str(obj.smtp_response or ''),
smart_str(obj.reporting_mta or '')])
# CONCLUDE
return response
示例5: ajax_4
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def ajax_4(request):
"""
浇水成功提交积分上去
:param request:
:return:
"""
response = HttpResponse()
response['Content-Type'] = 'application/json'
user_id = request.POST.get('openid', '')
source_id = request.POST.get('source_id', '')
if user_id:
user = User.objects.get(openid=user_id)
if user_id == source_id:
type = 0
else:
type = 3
Tree(owner=user, tree_name=user.tree_name, count=user.count, type=type,
action_time=time.time(), source_id=source_id,
content=User.objects.get(openid=source_id).nickname+'给树木浇水了').save()
user.count = user.count + 1000
if source_id:
try:
friend = user.friends.get(openid=source_id)
except ObjectDoesNotExist:
friend = 0
if friend != 0:
user.friends.add(User.objects.get(openid=source_id)) # 通过朋友圈啊什么的浇水,自己浇水的时候自己是自己的朋友
user.save()
ret = '1'
else:
ret = '2'
response.write(ret)
return response
示例6: export_student_list
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def export_student_list(modeladmin, request, queryset):
import csv
from django.utils.encoding import smart_str
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=nexteria_zoznam_studentov.csv'
writer = csv.writer(response, csv.excel)
response.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
writer.writerow([
smart_str(u"Meno"),
smart_str(u"Priezvisko"),
smart_str(u"Email"),
smart_str(u"Telefon"),
smart_str(u"Datum nar."),
smart_str(u"Fakulta"),
smart_str(u"Rok zaciatku"),
smart_str(u"Level"),
smart_str(u"Skolne"),
])
for obj in queryset:
writer.writerow([
smart_str(obj.clovek.meno),
smart_str(obj.clovek.priezvisko),
smart_str(obj.clovek.email),
smart_str(obj.clovek.telefon_cislo),
smart_str(obj.datum_nar),
smart_str(obj.fakulta),
smart_str(obj.rok_zaciatku),
smart_str(obj.level),
smart_str(obj.skolne),
])
return response
示例7: book
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def book(request, book):
try:
book_path = get_book_path(book, request.META.get('REMOTE_ADDR', '0.0.0.0'))
except AccessDenied as e:
return HttpResponse(e.message + u' Ваш ip адрес: ' + request.META.get('REMOTE_ADDR', '0.0.0.0'))
if not book_path or not os.path.isfile(book_path):
raise Http404(u'Книга не найдена')
token1 = request.GET.get('token1')
xml = """\
<Document Version="1.0">\
<Source File="source.xml" URL="http://%s/dl/%s/draw/?part=Part0.zip&book=%s&version=1285566137"/>\
<FileURL>http://%s/dl/%s/draw/?part={part}&book=%s</FileURL>\
<Token1>%s</Token1>\
<Permissions><AllowCopyToClipboard>true</AllowCopyToClipboard><AllowPrint>true</AllowPrint></Permissions>\
</Document>""" % (request.META['HTTP_HOST'],book, book, request.META['HTTP_HOST'], book, book, token1)
zip_file_content = cStringIO.StringIO()
zip_file = ZipFile(zip_file_content, 'w')
zip_file.writestr('doc.xml', xml)
zip_file.close()
response = HttpResponse(content_type="application/zip")
response["Content-Disposition"] = "attachment; filename=%s.zip" % book
zip_file_content.seek(0)
response.write(zip_file_content.read())
return response
示例8: switch_contral
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def switch_contral(request):
response = HttpResponse()
incident = {}
try:
if request.method == 'POST':
# req = simplejson.loads(request.body)
incident["suo_id"] = request.POST['suo_id']
incident["gui_id"] = request.POST['gui_id']
incident["switch_id"] = request.POST['switch_id']
HOST='127.0.0.1'
PORT=21590
BUFSIZ=1024
ADDR=(HOST,PORT)
client=socket(AF_INET,SOCK_DGRAM)
while True:
client.sendto(incident)
data,ADDR=client.recvfrom(BUFSIZ)
if not data:
break
client.close()
except Exception,ex:
print Exception,":",ex
print "error"
response.write("error")
return response
示例9: ajax_8
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def ajax_8(request):
"""
输入心愿提交到服务器
:param request:
:return:
"""
response = HttpResponse()
response['Content-Type'] = 'text/javascript'
user_id = request.POST.get('openid', '')
will_con = request.POST.get('will_con', '')
ret = '0'
if user_id and will_con:
user = User.objects.get(openid=user_id)
will = Tree(owner=user, tree_name=user.tree_name, type=2, action_time=time.time(), read=True, source_id=user_id,
content=will_con)
if user.willing == 'none':
user.count = user.count + 20000
user.willing = 'yes'
will.save()
user.save()
ret = '1'
else:
ret = '2'
response.write(ret)
return response
示例10: vote
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def vote(request,image_id,value):
cookie_key = "image_%s"%image_id
days_expire = 1
if request.is_ajax and not request.COOKIES.get(cookie_key):
try:
user_ip = get_client_ip(request)
time_ago = datetime.datetime.now()-datetime.timedelta(days=days_expire)
Vote.objects.get(user_ip=user_ip,created__gt=time_ago,image_id=image_id)
except ObjectDoesNotExist:
response = HttpResponse()
try:
image = Image.objects.get(pk=image_id)
except ObjectDoesNotExist:
return response
vote = Vote()
vote.value = 1 if value=='up' else -1
vote.user_ip = user_ip
vote.image = image
vote.created = datetime.datetime.now()
vote.save()
image.rating = Vote.objects.filter(image=image)\
.aggregate(sum=Sum('value')).get('sum')
image.save()
recalc_sizes(image.miracle)
image =Image.objects.get(id=image.pk)
new_data = {'rating':image.rating,'id':image.pk,'size':image.size}
new_data_encoded = json.dumps(new_data)
response.write(new_data_encoded)
set_cookie(response,cookie_key,True,days_expire)
return response
return HttpResponse()
示例11: preview
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def preview(module_factory, conf_factory, dbm_factory, params):
# 1. 通过参数解析配置
preview_conf = params.get(ModuleCommon.p_preview_conf)
if not preview_conf:
return HttpResponse(u"参数错误")
conf_dict = preview_conf.get(ModuleCommon.k_conf_dict)
if not conf_dict:
return HttpResponse(u"参数错误")
params[ModuleCommon.k_conf_dict] = conf_dict
path_route = preview_conf.get(ModuleCommon.p_path_route)
if not path_route:
return HttpResponse(u"参数错误")
params[ModuleCommon.p_path_route] = path_route
params["db_id"] = preview_conf.get("db_id")
params["baike_id"] = preview_conf.get("baike_id")
params["section"] = preview_conf.get("section")
# 2. 跟view调用一致
try:
html_render = module_factory.base_loader.loader(module_factory, params)
http_response = HttpResponse()
html = module_factory.base_render.render(params.get(ModuleCommon.k_request), html_render)
http_response.write(html)
return http_response
except Exception as info:
return HttpResponse(u"参数错误")
示例12: post
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def post(request):
name = Names.objects.get(username=request.user)
ids = request.POST.getlist('id', None)
idstring = []
for i in ids:
project = AssetInfo.objects.get(id=i).project
project_obj = AssetProject.objects.get(projects=project)
hasperm = name.has_perm('read_assetproject', project_obj)
if hasperm:
idstring.append(i)
idstring2 = ','.join(idstring)
qs = AssetInfo.objects.extra(where=['id IN (' + idstring2 + ')']).all()
# return render_to_csv_response(qs)
fields = [
field for field in Asset._meta.fields
if field.name not in [
'date_created'
]
]
filename = 'assets.csv'
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
response.write(codecs.BOM_UTF8)
writer = csv.writer(
response,
dialect='excel',
quoting=csv.QUOTE_MINIMAL)
header = [field.verbose_name for field in fields]
writer.writerow(header)
for asset_ in qs:
data = [getattr(asset_, field.name) for field in fields]
writer.writerow(data)
return response
示例13: ajax_10
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def ajax_10(request):
"""
给别人吐槽
:param request:
注意此处的source_id是推送的人的id,就是谁触发事件的
:return:
"""
response = HttpResponse()
response['Content-Type'] = 'text/javascript'
user_id = request.POST.get('openid', '')
source_id = request.POST.get('source_id', '')
tucao_con = request.POST.get('tucao_con', '')
ret = '0'
if user_id and source_id and tucao_con:
user = User.objects.get(openid=user_id)
user.count = user.count - 8000
user.save()
tucao = Tree(owner=user, tree_name=user.tree_name, type=6, action_time=time.time(), read=False,
source_id=source_id, content=tucao_con)
tucao.save()
ret = '1'
else:
ret = '2'
response.write(ret)
return response
示例14: complaint_csv
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def complaint_csv(self, request, queryset):
# PREPARE
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=complaints.csv'
writer = csv.writer(response, csv.excel)
response.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
# PROCESS
writer.writerow([
smart_str(u"ID"),
smart_str(u"MESSAGE ID"),
smart_str(u"TIMESTAMP"),
smart_str(u"MAIL FROM"),
smart_str(u"MAIL TO"),
smart_str(u"USERAGENT"),
smart_str(u"COMPLAINT TYPE"),
smart_str(u"ARRIVAL DATE")])
for obj in queryset:
writer.writerow([
smart_str(obj.pk or ''),
smart_str(obj.sns_messageid or ''),
smart_str(obj.mail_timestamp or ''),
smart_str(obj.mail_from or ''),
smart_str(obj.address or ''),
smart_str(obj.useragent or ''),
smart_str(obj.feedback_type or ''),
smart_str(obj.arrival_date or '')])
# CONCLUDE
return response
示例15: bounce_csv
# 需要导入模块: from django.shortcuts import HttpResponse [as 别名]
# 或者: from django.shortcuts.HttpResponse import write [as 别名]
def bounce_csv(self, request, queryset):
# PREPARE
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=bounces.csv'
writer = csv.writer(response, csv.excel)
# PROCESS
response.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
writer.writerow([
smart_str(u"ID"),
smart_str(u"MESSAGE ID"),
smart_str(u"TIMESTAMP"),
smart_str(u"MAIL FROM"),
smart_str(u"MAIL TO"),
smart_str(u"BOUNCE TYPE"),
smart_str(u"BOUNCE SUBTYPE"),
smart_str(u"REPORTING MTA"),
smart_str(u"ACTION"),
smart_str(u"STATUS"),
smart_str(u"DIAGNOSTIC CODE")])
for obj in queryset:
writer.writerow([
smart_str(obj.pk or ''),
smart_str(obj.sns_messageid or ''),
smart_str(obj.mail_timestamp or ''),
smart_str(obj.mail_from or ''),
smart_str(obj.address or ''),
smart_str(obj.bounce_type or ''),
smart_str(obj.bounce_subtype or ''),
smart_str(obj.reporting_mta or ''),
smart_str(obj.action or ''),
smart_str(obj.status or ''),
smart_str(obj.diagnostic_code or '')])
# CONCLUDE
return response