本文整理汇总了Python中django.http.response.HttpResponse.content方法的典型用法代码示例。如果您正苦于以下问题:Python HttpResponse.content方法的具体用法?Python HttpResponse.content怎么用?Python HttpResponse.content使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.http.response.HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.content方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render_pdf
# 需要导入模块: from django.http.response import HttpResponse [as 别名]
# 或者: from django.http.response.HttpResponse import content [as 别名]
def render_pdf( self, html, filename = None ):
response = HttpResponse( content_type = self.PDF_CNT_TYPE )
w = Watch( "BaseView.render_pdf" )
w.start()
if filename == None:
filename = datetime.now().strftime( "%Y%m%d" ) + '-export.pdf'
pdf_path = settings.MEDIA_ROOT + '/pdf/contracts/' + filename
pdf_file = open( pdf_path, "w+b" )
try:
pisa.CreatePDF( html, dest = pdf_file )
except Exception as e :
print ( traceback.format_exc() )
pdf_file.seek( 0 )
pdf = pdf_file.read()
pdf_file.close()
response['Content-Disposition'] = filename
response["Cache-Control"] = "max-age=0"
response["Accept-Ranges"] = "none"
response.content = pdf
print( "Rendering PDF took :" )
print( w.display() )
return response
示例2: post
# 需要导入模块: from django.http.response import HttpResponse [as 别名]
# 或者: from django.http.response.HttpResponse import content [as 别名]
def post(self, request, *args, **kwargs):
"""
Doesn't allow requests from localhost.
If form data is correct, function adds new Note object.
If not, returns form with errors.
Allows CORS.
"""
response = HttpResponse()
response['Content-Type'] = 'application/json'
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Credentials'] = 'true'
response['Access-Control-Allow-Methods'] = ', '.join(self.default_methods)
response['Access-Control-Allow-Headers'] = ', '.join(self.default_headers)
if request.META['REMOTE_ADDR'] == '127.0.0.1':
content = {'result': 'error', 'msg': 'POST is not allowed from localhost.'}
else:
form = AddNoteForm(request.POST, request.FILES)
if form.is_valid():
text = form.cleaned_data['text']
img = form.cleaned_data['img']
Note.objects.create(text=text, img=img)
content = {'result': 'success', 'redirect': reverse('note_list')}
else:
t = loader.get_template('add_note_form.html')
ctx = RequestContext(request, {'form': form})
form_with_errors = t.render(ctx)
content = {'result': 'success', 'form': unicode(form_with_errors)}
response.content = json.dumps(content)
return response
示例3: make_response
# 需要导入模块: from django.http.response import HttpResponse [as 别名]
# 或者: from django.http.response.HttpResponse import content [as 别名]
def make_response(status=200, content=None):
if content is None:
content = {}
response = HttpResponse()
response.status_code = status
response['Content-Type'] = "application/json"
response.content = json.dumps(content)
return response
示例4: get
# 需要导入模块: from django.http.response import HttpResponse [as 别名]
# 或者: from django.http.response.HttpResponse import content [as 别名]
def get(self, request):
resp = HttpResponse()
pid = request.session.get('pid', None)
if not pid:
resp.content = ugettext("No active process to kill")
else:
os.kill(pid, signal.SIGINT)
i = 0
while i <= settings.SCRIPT_MAX_WAIT and check_pid(pid):
time.sleep(settings.SCRIPT_WAIT)
i += 1
if check_pid(pid):
os.killpg(pid, signal.SIGKILL)
resp.content = ugettext("Success: The process was killed successfully.")
return resp
示例5: get
# 需要导入模块: from django.http.response import HttpResponse [as 别名]
# 或者: from django.http.response.HttpResponse import content [as 别名]
def get(self, request):
response = HttpResponse()
response['Content-Type'] = 'application/json'
response['Access-Control-Allow-Origin'] = '*'
if Note.objects.count() < 1:
content = {'result': 'failure', 'msg': 'No text notes'}
else:
t = loader.get_template('note.html')
ctx = RequestContext(request, {'note': self.get_random_note()})
random_note_repr = t.render(ctx)
content = {'result': 'success', 'random_note': unicode(random_note_repr)}
response.content = json.dumps(content)
return response
示例6: post
# 需要导入模块: from django.http.response import HttpResponse [as 别名]
# 或者: from django.http.response.HttpResponse import content [as 别名]
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
job = form.save(commit=False)
left = default_storage.open(job.left_table.handle.name)
right = default_storage.open(job.right_table.handle.name)
left = csv.DictReader(left)
right = csv.DictReader(right)
keys = left.fieldnames
for key in right.fieldnames:
if key not in keys:
keys.append(key)
keys = sorted(keys)
left = [r for r in left]
right = [r for r in right]
union = left + right
path = os.path.join(BASE_DIR, MEDIA_ROOT, 'catalogs', '{}.csv'.format(job.results_label))
handler = open(path,'w')
handler = csv.DictWriter(handler, fieldnames=keys)
handler.writeheader()
handler.writerows(union)
job.msg_json = union
job.job_status = "complete"
job.completed = True
job.results_handle = 'catalogs/{}.csv'.format(job.results_label)
response = HttpResponse()
response['content-type'] = 'text/plain'
# data['left_table'] = job.left_table.handle.name
# data['left_content'] = default_storage.exists(job.left_table.handle.name)
data = json.dumps(union, indent=2)
result = Catalog(
name=job.results_label,
json=data,
handle='catalogs/{}.csv'.format(job.results_label)
)
# result.handle.save('catalogs/joined_csv.csv', union)
result.save()
response.content = data
job.save()
return response
# return redirect(reverse('jobs:index'))
return render(request, self.template_name, {'form': form})
示例7: get
# 需要导入模块: from django.http.response import HttpResponse [as 别名]
# 或者: from django.http.response.HttpResponse import content [as 别名]
def get(self, request, *args, **kwargs):
response = HttpResponse(content_type=self.content_type)
geom = self.kwargs.get('geometry_field', 'geom')
geom_type = 'stations'
if geom == 'district':
geom_type = 'districts'
department = get_object_or_404(FireDepartment, id=kwargs['pk'])
queryset = self.get_queryset(department=department)
filename = self.filename(department=department, geom_type=geom_type)
file_path, zip_file = self.create_shapefile(queryset, filename, geom)
response['Content-Disposition'] = 'attachment; filename="{0}.zip"'.format(smart_str(filename))
response['X-Accel-Redirect'] = smart_str(zip_file)
response.content = file_path
return response
示例8: file_as_image
# 需要导入模块: from django.http.response import HttpResponse [as 别名]
# 或者: from django.http.response.HttpResponse import content [as 别名]
def file_as_image(path):
response = HttpResponse(content_type=mimetypes.guess_type(path))
with open(path, 'rb') as cover_file:
response.content = cover_file.read()
response['Content-Length'] = len(response.content)
return response