diff --git a/docs/45.md b/docs/45.md index bbec644156b8b5804a7b2aa75025e71ccf26432f..05c8574d68dcf31806f06c45f7c3c9cb24cd1aec 100644 --- a/docs/45.md +++ b/docs/45.md @@ -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 diff --git a/docs/55.md b/docs/55.md index 7091e574afc8ce8ddc2049ec7410aa2ac0e713ef..fed5e97ff63eb02c6c1abab0be464a1e3c411950 100644 --- a/docs/55.md +++ b/docs/55.md @@ -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 %} + + +
+
+ + + +{% endblock %} ``` 结果: diff --git a/docs/64.md b/docs/64.md index 746d0649e150c83c17d5c40506419d62d6066ee7..f3d3139322c95dc52b6969871f79ec27b4456393 100644 --- a/docs/64.md +++ b/docs/64.md @@ -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 - +Hello ``` 打开 [http://127.0.0.1:8000/](http://127.0.0.1:8000/) 进行测试。 将`note.html`更改为: ```py -1 -2 -3 -4 -5 - +

Notes

+ ``` 然后打开`/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 - +

Notes

+ +
{% csrf_token %} +{ { form.as_p }} + +
``` 运行它,我们有记笔记的应用程序 :-) @@ -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 + + +

Django Note Taking App

+ + +
{% csrf_token %} +{ { form.as_p }} + +
``` diff --git a/docs/77.md b/docs/77.md index 6c032217ceadb803df9303e6602cda2b9a7442f9..dd397ca8d8a7a12c430fd2adb62a5fac2ad1e2e1 100644 --- a/docs/77.md +++ b/docs/77.md @@ -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`):