提交 c4fb071c 编写于 作者: W wizardforcel

2019-11-21 11:30:31

上级 9044e338
......@@ -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
......@@ -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 %}
```
结果:
......
......@@ -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 %&#125;
<li>&#123; &#123; note.text &#125;&#125;</li>
&#123;% endfor %&#125;</ul>
<form method="POST" action="">&#123;% csrf_token %&#125;
&#123; &#123; form.as_p &#125;&#125;
<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: 20px;<br />
}<br />
</style>
<h1>Django Note Taking App</h1>
<form method="POST" action="">&#123;% csrf_token %&#125;
{ { form.as_p }}
<input type="submit" value="Add note">
</form>
```
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册