Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
pythonspot-zh
提交
c4fb071c
P
pythonspot-zh
项目概览
OpenDocCN
/
pythonspot-zh
8 个月 前同步成功
通知
0
Star
46
Fork
11
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
pythonspot-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c4fb071c
编写于
11月 21, 2019
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
2019-11-21 11:30:31
上级
9044e338
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
171 addition
and
203 deletion
+171
-203
docs/45.md
docs/45.md
+6
-6
docs/55.md
docs/55.md
+36
-37
docs/64.md
docs/64.md
+125
-155
docs/77.md
docs/77.md
+4
-5
未找到文件。
docs/45.md
浏览文件 @
c4fb071c
...
...
@@ -66,9 +66,9 @@ CREATE TABLE IF NOT EXISTS examples (
然后,我们可以将数据插入表中(这些是 SQL 查询):
```
py
1
2
3
INSERT
INTO
examples
(
description
)
VALUES
(
"Hello World"
);
INSERT
INTO
examples
(
description
)
VALUES
(
"MySQL Example"
);
INSERT
INTO
examples
(
description
)
VALUES
(
"Flask Example"
);
```
...
...
@@ -115,8 +115,8 @@ for row in cur.fetchall() :
输出:
```
py
1
Hello
World
2
MySQL
Example
3
Flask
Example
Hello
World
MySQL
Example
Flask
Example
```
\ No newline at end of file
docs/55.md
浏览文件 @
c4fb071c
...
...
@@ -75,43 +75,42 @@ app.run()
我们有这个模板:
```
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{
%
block
body
%
}
<
script
type
=
"text/javascript"
src
=
"https://www.google.com/jsapi"
></
script
>
<
div
id
=
"chart_div"
style
=
"width: 900px; height: 300px;"
>
<
div
>
<
script
type
=
"text/javascript"
>//<
!
[
CDATA
[
google
.
load
(
'visualization'
,
'1'
,
{
packages
:
[
'corechart'
,
'bar'
]});
<
br
/>
google
.
setOnLoadCallback
(
drawBasic
);
</
p
>
<
p
>
function
drawBasic
()
{
</
p
>
<
p
>
var
data
=
google
.
visualization
.
arrayToDataTable
([
<
br
/>
[
'Currency'
,
'Rate'
,
{
role
:
'style'
}],
<
br
/>
[
'USD'
,
{
{
rates
[
0
]}},
'gold'
],
<
br
/>
[
'GBP'
,
{
{
rates
[
1
]}},
'silver'
],
<
br
/>
[
'HKD'
,
{
{
rates
[
2
]}},
'brown'
],
<
br
/>
[
'AUD'
,
{
{
rates
[
3
]}},
'blue'
]
<
br
/>
]);
</
p
>
<
p
>
var
options
=
{
<
br
/>
title
:
'Exchange rate overview'
,
<
br
/>
chartArea
:
{
width
:
'50%'
},
<
br
/>
hAxis
:
{
<
br
/>
title
:
''
,
<
br
/>
minValue
:
0
<
br
/>
},
<
br
/>
vAxis
:
{
<
br
/>
title
:
''
<
br
/>
}
<
br
/>
};
</
p
>
<
p
>
var
chart
=
new
google
.
visualization
.
BarChart
(
document
.
getElementById
(
'chart_div'
));
</
p
>
<
p
>
chart
.
draw
(
data
,
options
);
<
br
/>
}
<
br
/>
//
]]
>
</
p
>
<
p
></
script
>
{
%
endblock
%
}
```
结果:
...
...
docs/64.md
浏览文件 @
c4fb071c
...
...
@@ -19,41 +19,36 @@ django-admin startproject mysite
这将创建目录
`mysite`
。 打开
`mysite/mysite/settings.py`
。您可以在此处配置所需的数据库:
```
py
1
2
3
4
5
6
7
DATABASES
=
{
'default'
:
{
'ENGINE'
:
'django.db.backends.sqlite3'
,
'NAME'
:
os
.
path
.
join
(
BASE_DIR
,
'db.sqlite3'
),
}
}
```
从
`sqlite3`
,
`postgresql_psycopg2`
,
`mysql`
或
`oracle`
中选择。 名称是您的数据库的名称。 如果您使用 SQLite,则会自动创建数据库。 对于 MySQL 和 Postgresql,您需要自己创建数据库。 将一个目录转到
`/mysite/`
并运行:
```
py
1
2
python
manage
.
py
runserver
```
终端应该说:
```
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Performing
system
checks
...
System
check
identified
no
issues
(
0
silenced
).
You
have
unapplied
migrations
;
your
app
may
not
work
properly
until
they
are
applied
.
Run
'python manage.py migrate'
to
apply
them
.
August
16
,
2015
-
14
:
45
:
29
Django
version
1.7
.
1
,
using
settings
'myapp.settings'
Starting
development
server
at
http
:
//
127.0
.
0.1
:
8000
/
Quit
the
server
with
CONTROL
-
C
.
[
16
/
Aug
/
2015
14
:
45
:
35
]
"GET / HTTP/1.1"
200
1759
```
...
...
@@ -88,27 +83,25 @@ notes/
将
`/mysite/notes/models.py`
更改为:
```
py
1
2
3
4
5
6
from
django.db
import
models
class
Note
(
models
.
Model
):
text
=
models
.
CharField
(
max_length
=
120
)
created
=
models
.
DateTimeField
(
auto_now_add
=
True
)
```
打开
`/mysite/mysite/settings.py`
,添加 Web 应用程序:
```
py
1
2
3
4
5
6
7
8
9
INSTALLED_APPS
=
(
'django.contrib.admin'
,
'django.contrib.auth'
,
'django.contrib.contenttypes'
,
'django.contrib.sessions'
,
'django.contrib.messages'
,
'django.contrib.staticfiles'
,
'notes'
)
```
...
...
@@ -122,26 +115,24 @@ python manage.py syncdb
这将更新数据库。 然后,我们将
`/mysite/mysite/admin.py`
更新为:
```
py
1
2
3
4
5
6
7
8
9
10
11
from
django.contrib
import
admin
# Register your models here.
from
.models
import
Note
class
NoteAdmin
(
admin
.
ModelAdmin
):
class
Meta
:
model
=
Note
admin
.
site
.
register
(
Note
,
NoteAdmin
)
```
运行:
```
py
1
2
python
manage
.
py
makemigrations
notes
python
manage
.
py
migrate
```
使用以下命令启动服务器:
...
...
@@ -168,14 +159,13 @@ Django 数据库管理菜单。
我们将所有数据存储在数据库中,现在我们要创建应用程序。 打开
`/mysite/settings.py`
并添加:
```
py
1
2
3
4
5
6
7
#print "base dir path", BASE_DIR
#print os.path.join(os.path.dirname(BASE_DIR), "mysite", "static", "templates")
TEMPLATE_DIRS
=
(
os
.
path
.
join
(
os
.
path
.
dirname
(
BASE_DIR
),
"mysite"
,
"static"
,
"templates"
),
#'/home/frankbox/python/djangoapp/mysite/static/templates',
)
```
到文件底部。 这定义了模板的目录(html)。
...
...
@@ -183,60 +173,49 @@ Django 数据库管理菜单。
将
`/mysite/mysite/urls.py`
更改为:
```
py
1
2
3
4
5
6
7
8
9
10
11
from
django.conf.urls
import
patterns
,
include
,
url
from
django.contrib
import
admin
urlpatterns
=
patterns
(
''
,
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url
(
r
'^$'
,
'notes.views.home'
,
name
=
'home'
),
url
(
r
'^admin/'
,
include
(
admin
.
site
.
urls
)),
)
```
最后,创建
`/mysite/static/templates/`
并添加
`note.html`
,这是一个简单的静态 html 文件。
```
py
1
2
3
4
5
<
b
>
Hello
</
b
>
```
打开
[
http://127.0.0.1:8000/
](
http://127.0.0.1:8000/
)
进行测试。 将
`note.html`
更改为:
```
py
1
2
3
4
5
<
h2
>
Notes
</
h2
>
<
ul
>
</
ul
>
```
然后打开
`/mysite/notes/views.py`
并更改为:
```
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from
django.shortcuts
import
render
,
render_to_response
,
RequestContext
from
django.template
import
RequestContext
,
loader
from
django.http
import
HttpResponse
from
.models
import
Note
# Create your views here.
def
home
(
request
):
notes
=
Note
.
objects
template
=
loader
.
get_template
(
'note.html'
)
context
=
{
'notes'
:
notes
}
return
render
(
request
,
'note.html'
,
context
)
#return render_to_response("note.html", notes)
```
启动浏览器后,您将看到笔记列表:
...
...
@@ -250,58 +229,51 @@ django 应用
虽然有一个列表很不错,但我们想在其中添加一些注释。创建文件
`/mysite/notes/forms.py`
```
py
1
2
3
4
5
6
7
8
from
django
import
forms
from
.models
import
Note
class
NoteForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
Note
```
将
`view.py`
更改为:
```
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from
django.shortcuts
import
render
,
render_to_response
,
RequestContext
from
django.template
import
RequestContext
,
loader
from
django.http
import
HttpResponse
from
.models
import
Note
from
.forms
import
NoteForm
# Create your views here.
def
home
(
request
):
notes
=
Note
.
objects
template
=
loader
.
get_template
(
'note.html'
)
form
=
NoteForm
(
request
.
POST
or
None
)
if
form
.
is_valid
():
save_it
=
form
.
save
(
commit
=
False
)
save_it
.
save
()
context
=
{
'notes'
:
notes
,
'form'
:
form
}
return
render
(
request
,
'note.html'
,
context
)
#return render_to_response("note.html", notes)
```
最后,我们将
`note.html`
更新为:
```
py
1
2
3
4
5
6
7
8
9
10
11
<
h2
>
Notes
</
h2
>
<
ul
>
&
#123;% for note in notes.all %}
<
li
>&
#123; { note.text }}</li>
&
#123;% endfor %}</ul>
<
form
method
=
"POST"
action
=
""
>&
#123;% csrf_token %}
&
#123; { form.as_p }}
<
input
type
=
"submit"
>
</
form
>
```
运行它,我们有记笔记的应用程序 :-)
...
...
@@ -315,22 +287,20 @@ Djano 记笔记的应用
通过修改
`note.html`
,我们可以像其他任何 html/css 网站一样设置其样式。 如果将
`note.html`
更改为:
```
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<
link
href
=
"http://codepen.io/edbond88/pen/CcgvA.css"
media
=
"screen"
rel
=
"stylesheet"
type
=
"text/css"
>
<
style
>
body
{
<
br
/>
background
:
rgba
(
222
,
222
,
222
,
1
);
<
br
/>
margin
:
20
px
;
<
br
/>
}
<
br
/>
</
style
>
<
h1
>
Django
Note
Taking
App
</
h1
>
<
form
method
=
"POST"
action
=
""
>&
#123;% csrf_token %}
{
{
form
.
as_p
}}
<
input
type
=
"submit"
value
=
"Add note"
>
</
form
>
```
...
...
docs/77.md
浏览文件 @
c4fb071c
...
...
@@ -2,7 +2,7 @@
> 原文: [https://pythonspot.com/building-an-irc-bot/](https://pythonspot.com/building-an-irc-bot/)
有
[
IRC(
Internet
中继聊天)
](
https://en.wikipedia.org/wiki/Internet_Relay_Chat
)
的大量(ro)机器人。 那么,如何仅仅为了好玩而开始在 Python 中构建和构建一个?
有
[
IRC(
互联网
中继聊天)
](
https://en.wikipedia.org/wiki/Internet_Relay_Chat
)
的大量(ro)机器人。 那么,如何仅仅为了好玩而开始在 Python 中构建和构建一个?
您将需要一个与 IRC 服务器连接并像传统 IRC 客户端一样运行的程序。IRC 服务器从不要求进行任何复杂的人工验证,例如解决验证码,这就是为什么我们可以简单地使用脚本进行连接的原因。该脚本本身将使用网络套接字,一个库,该库通常用于以多种编程语言(包括 Python 和 C / C++)提供网络交互。
...
...
@@ -20,10 +20,9 @@ irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
`socket.AF_INET`
通知库使用网络协议
[
IPv4
](
https://en.wikipedia.org/wiki/IPv4
)
。第二个参数告诉库使用流套接字,该套接字通常在 TCP 协议上实现。(IRC 通过 TCP/IP 进行工作)。 然后,我们必须使用以下命令对服务器进行身份验证:
```
py
1
2
3
USER
botname
botname
botname
:
phrase
NICK
botname
JOIN
#channel
```
有时
`IDENT`
命令也是必要的。 总结一下,我们得到了这个类(另存为
`irc.py`
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录