Bean's Space

死了,就像水消失在水中。


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

  • 日程

  • 公益404

Django-HttpResponse、render、redirect区别 及 url 变化区别

发表于 2020-05-01   |  

@TOC

HttpResponse

作用为内部传递一个参数,返回给浏览器

1
2
3
4
5
from django.shortcuts import HttpResponse
def from_request(request):
# Todo
return HttpResponse('OK')

render

可接受三个参数,分别为请求request, 需要渲染的模板template,以及向模板传递的参数context,render(request, ‘template’, context)
原型

1
2
3
def render(request, template_name, context=None, content_type=None, status=None, using=None):
content = loader.render_to_string(template_name, context, request, using=using)
return HttpResponse(content, content_type, status)

使用方式

1
2
3
4
5
6
7
from django.shotcuts import render
def from_request(request):
# Todo
# return render(request, 'template.html', context)
# or
return render(request, 'template.html')

redirect

接受一个URL参数,表示让浏览器跳转去指定的URL.
原型

1
2
3
def redirect(to, *args, permanent=False, **kwargs):
redirect_class = HttpResponsePermanentRedirect if permanent else HttpResponseRedirect
return redirect_class(resolve_url(to, *args, **kwargs))

使用方式

1
2
3
4
5
from django.shotcuts import redirect
def from_request(request):
# Todo
return redirect(request, 'page')

render 与redirect 时url变化区别

urls.py中绑定了你跳转时浏览器显示的url

初学时经常使用render的形式,发现url显示为当前request绑定的url,如果处理请求后结束后渲染了新的页面不过Url并没有跳转。
例如

1
2
3
4
5
6
7
8
9
10
11
12
# urls.py
urlpatterns=[
path('from_request/', views.from_request, name='from_request')
]
# views.py
def from_request(request):
if condition1:
return render(request, 'page1.html')
else
return render(request, 'page2.html')

此时无论跳转page1还是page2 url的显示都会是 ==yoursite/from_request==

这可以归类为设计的问题,及绑定的url就应该是目的渲染的html
但如果想要跳转时url也跟着改变即可采用redirect方式重新返回绑定的url
例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# urls.py
urlpatterns=[
path('from_request/', views.from_request, name='from_request'),
path('to_page1/', views.to_page1, name='to_page1'),
path('to_page2/', views.to_page2, name='to_page2'),
]
# views.py
def from_request(request):
if condition1:
return redirect('to_page1/')
else
return redirect('to_page2/')

CNN学习笔记

发表于 2016-11-07   |   分类于 深度学习   |  

神经元模型介绍

神经网络中最基本的成分是神经元(neuron)模型。在生物神经网络中,每个神经元与其他神经元相连,当它“兴奋”时,就会像相连的神经元发送化学物质,从而改变这些神经元的电位;如果某神经元的电位超过了一个“阈值”(threshold),那么它就会被激活,即“兴奋”起来,像其他神经元发送化学物质。
将上述情形抽象一下,就得到了一直沿用至今的“M-P神经元模型“,在这个模型中,神经元接收来自n个其他神经元传递过来的输入信号,这些输入信号通过带权重的连接(connection)进行传递,神经元接收到的总输入值将于神经元的阈值进行比较,然后通过”激活函数“(activation function)处理以产生神经元的输出。

阅读全文 »

关于 git push 每次都需要输入用户名和密码的问题

发表于 2016-11-04   |  

git push每次都需要输入用户名和密码是因为你采用的是 https 方式提交代码, 如果采用的是 ssh 方式只需要在版本库中添加用户的 sha 的key就可以实现提交时无需输入用户名和密码。

解决方案:

详细步骤:

如果你的版本库已经用https 方式创建好了,那么就需要先删除原来的提交方式。在终端执行以下指令:

1
2
git remote rm origin
git remote add origin git@github.com:(用户名)/版本库名

阅读全文 »

Mac 环境下安装 MongoDB

发表于 2016-10-08   |  

由于要学习分布式数据库,所以下载了MongoDB,过程中遇到了一些坑,因此记录下来。

下载与安装:

这个没什么,通过 Homebrew 来就好了,首先更新下你的 Homebrew :

1
$brew update

更新完毕后采用下面的命令安装 MongoDB:

1
$brew install mongodb

当然也可以安装支持 TSL/SSL 的版本:

1
$brew install mongodb --with-openssl

或者开发版:

1
$brew install mongodb --devel

MongoDB 的大小在270M左右,一会就下好了。

阅读全文 »

LeetCode 371.Sum of Two Integers

发表于 2016-07-25   |   分类于 刷题   |  

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example:

Given a = 1 and b = 2, return 3.

这本是一道简单的a+b求和问题,但是不准用到+和-,所以我们考虑使用位运算。

阅读全文 »
123
Bean.cb

Bean.cb

中二青年

15 日志
4 分类
7 标签
github twitter weibo zhihu
© 2015 - 2020 Bean.cb
由 Hexo 强力驱动
主题 - NexT.Mist