Django 不完全入坑填坑记录


Invalid HTTP_HOST header: ... You may need to add ... to ALLOWED_HOSTS.

前段时间玩了下Django,我就想着今天用来写下API,为了让AVD可以访问,所以用了10.0.2.2这个上帝IP,结果后台显示

[25/Jan/2018 09:48:08] "GET /okhttp/ HTTP/1.1" 400 60225
Invalid HTTP_HOST header: '10.0.2.2:8000'. You may need to add u'10.0.2.2' to ALLOWED_HOSTS.

谷歌了下得到解决方案:
在项目中找到settings.py,并且添加ALLOWED_HOSTS 例如:

ALLOWED_HOSTS = ['198.211.99.20', 'localhost', '127.0.0.1']

原文:https://stackoverflow.com/questions/40582423/invalid-http-host-header

Django通过URL传递参数的4种方法以及POST请求获取参数

1.无参数情况

配置URL:(r'^hello/$', hello)
视图:

def hello(request):
    return HttpResponse("Hello World")

访问http://127.0.0.1:8000/hello,输出结果为“Hello World”

2.传递一个参数

配置URL,URL中通过正则指定一个参数:(r'^plist/(.+)/$', helloParam)
视图:

def helloParam(request,param1):
    return HttpResponse("The param is : " + param1)

访问http://127.0.0.1:8000/plist/china,输出结果为”The param is : china”

3.传递多个参数

参照第二种情况,以传递两个参数为例,配置URL,URL中通过正则指定两个参数:
(r'^plist/p1(\w+)p2(.+)/$', helloParams)
视图:

def helloParams(request,param1,param2):
     return HttpResponse("p1 = " + param1 + "; p2 = " + param2)

访问http://127.0.0.1:8000/plist/p1chinap22012/ ,输出为”p1 = china; p2 = 2012″

4.通过传统的”?”传递参数(GET表单)

例如,http://127.0.0.1:8000/plist/?p1=china&p2=2012,url中‘?’之后表示传递的参数,这里传递了p1和p2两个参数。
通过这样的方式传递参数,就不会出现因为正则匹配错误而导致的问题了。在Django中,此类参数的解析是通过request.GET.get方法获取的。
配置URL:(r'^plist/$', helloParams1)
视图:

def helloParams(request):
    p1 = request.GET.get('p1')
    p2 = request.GET.get('p2')
    return HttpResponse("p1 = " + p1 + "; p2 = " + p2)

输出结果为”p1 = china; p2 = 2012″

参考:http://www.icodelogic.com/?p=501

5.传统的POST表单

URL部分:url(r'^search-post$', search_post)
Views部分:

def search_post(request):
    resp=request.POST['q']
    return HttpResponse(resp)

POST中传一个参数q,值为helloPost,那么输出为:helloPost,与GET类似,已经被封装好了,所以大体上也是一致的。

参考:http://www.runoob.com/django/django-form.html

Django使用本机IP进行访问或者使局域网中其他设备访问

在启动命令后面加上 0.0.0.0:端口

比如我的:

./manage.py runserver 0.0.0.0:8000

这样就开启了开发者模式 django dev server

然后就可以用内网的ipv4地址访问了

Django获取HTTP请求头信息

HttpRequest.META
A standard Python dictionary containing all available HTTP headers. Available headers depend on the client and server, but here are some examples:

CONTENT_LENGTH – the length of the request body (as a string).
CONTENT_TYPE – the MIME type of the request body.
HTTP_ACCEPT_ENCODING – Acceptable encodings for the response.
HTTP_ACCEPT_LANGUAGE – Acceptable languages for the response.
HTTP_HOST – The HTTP Host header sent by the client.
HTTP_REFERER – The referring page, if any.
HTTP_USER_AGENT – The client’s user-agent string.
QUERY_STRING – The query string, as a single (unparsed) string.
REMOTE_ADDR – The IP address of the client.
REMOTE_HOST – The hostname of the client.
REMOTE_USER – The user authenticated by the Web server, if any.
REQUEST_METHOD – A string such as "GET" or "POST".
SERVER_NAME – The hostname of the server.
SERVER_PORT – The port of the server (as a string).

With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted toMETA keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.

用法:

print('Header:')
print('SERVER_NAME:' + request.META.get('SERVER_NAME', 'unknown'))
print('REQUEST_METHOD:' + request.META.get('REQUEST_METHOD', 'unknown'))
print('HTTP_ACCEPT_LANGUAGE:' + request.META.get('HTTP_ACCEPT_LANGUAGE', 'unknown'))
print('HTTP_HOST:' + request.META.get('HTTP_HOST', 'unknown'))
print('HTTP_USER_AGENT:' + request.META.get('HTTP_USER_AGENT', 'unknown'))

这样就可以输出一部分常用的了,其他的按文档替换即可,第二个参数是指当无数据时的默认值

TypeError: unsupported operand type(s) for +: 'int' and 'str'

“类型错误:不支持操作类型为整数和字符串”,这里需要解释的最关键的东西是“+”,“+”在python中有两个作用,一个是数学运算符,是用来在整型、浮点型等数学之间进行加法操作的。另一个是用来进行字符串连接的。所以当你的“+”出现在即有数学运算和字符连接的情况下,计算机根本不知道哪两个是要进行字符串连接,哪两个之间要进行数学运算。

所以(name为一个变量):

name + " edit success",

改为

str(name) + " edit success",

参考:http://blog.csdn.net/foryouslgme/article/details/51536882

声明:TIL|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA[ZH]协议进行授权

转载:转载请注明原文链接 - Django 不完全入坑填坑记录


Life is very interesting. In the end, some of your greatest pains become your greatest strengths.