For the rest of this lesson, you
are going to be working on Python code
that should already be in the forum
sub-directory of your vagrant directory.
You can edit this code with your
favorite text editor on your computer,
and when you save it,
those changes will be visible
from inside the virtual machine.
So, what is this code?
It's a web forum,
just not a very good one yet.
You could run it like any Python
program from the command line.
And once it starts up, it'll tell you
it's listening on port 8000 and then you
can access it at http://localhost:8000
in your web browser.
And when you do,
you'll see something like this.
Hm, it's pretty empty.
Let's put some posts in it.
Now, if we go back to our terminal, and
kill the web server with Ctrl C,
and then restart it and
go back to the web browser and
reload all our posts are gone.
What's going on there?
Now, if we look over in the source code
for this module that forum.py is using,
we'll, see, it's not actually
using a database at all.
It's faking it with plain old variables
which go away when the program exits.
So for the next few exercises,
you're going to be adapting this code to
use a database to make posts persistent.
First off,
let's say hello to the database system
that we're going to use, PostgreSQL.
،لما تبقى من هذا الدرس
ستعمل على تعليمة Python البرمجية
والتي ينبغي أن تكون في الدليل الفرعي
.لمنتدى دليل vagrant لديك بالفعل
يمكنك تحرير التعليمة البرمجية
،باستخدام محرر النصوص المفضل لديك على الكمبيوتر
،وعند حفظها
سيتم عرض هذه التغييرات
.من داخل الجهاز الظاهري
حسنًا، ما هذه التعليمة البرمجية؟
،هي عبارة عن منتدى ويب
.ولكنه ليس جيدًا جدًا حتى الآن
يمكنك تشغيله مثل أي برنامج
.Python من سطر الأوامر
وبمجرد أن يبدأ تشغيله، سيخبرك
أنه يتصل بالمنفذ 8000 ثم يمكنك
الوصول إليه من خلال http://localhost:8000
.في مستعرض الويب
،وعند قيامك بذلك
.سترى شيئًا كهذا
.اممم، إنه فارغ
.لنضف بعض المنشورات إليه
والآن، إذا رجعنا إلى الوحدة الطرفية
،وأنهينا تشغيل خادم الويب باستخدام الأمر Ctrl C
ثم قمنا بإعادة تشغيله
ورجعنا إلى مستعرض الويب
.وإعادة التحميل، نرى أن كل المنشورات قد اختفت
ما الذي يجرى هنا؟
والآن، إذا نظرنا إلى التعليمة البرمجية المصدر
،لهذه الوحدة النمطية التي يستخدمها forum.py
سنرى أنها لا تستخدم فعليًا
.قاعدة بيانات على الإطلاق
بل تدعي استخدامها عن طريق متغيرات قديمة عادية
.والتي تختفي عند الخروج من البرنامج
لذا، في التمارين القليلة القادمة
ستقوم بتهيئة هذه التعليمة البرمجية
.لاستخدام قاعدة بيانات من أجل المحافظة على استدامة المنشورات بها
،أول شيء
لنلق التحية على نظام قاعدة البيانات
.الذي سنستخدمه، PostgreSQL
No restante desta lição, você
vai trabalhar no código Python
que já deve estar no subdiretório de
seu diretório vagrant no fórum.
Você pode editar esse código com seu editor de texto
favorito em seu computador
e, ao salvá-lo,
essas mudanças estarão visíveis
na máquina virtual.
O que é esse código?
É um fórum da Web,
mas ainda não está muito bom.
Você poderia executá-lo em qualquer programa Python
na linha de comando.
E, ao iniciar, ele informa
que está ouvindo na porta 8000 e você pode
acessá-lo em http://localhost:8000
no navegador da Web.
E, ao fazer isso,
você verá algo assim.
Está bem vazio.
Vamos colocar algumas postagens nele.
Agora, se voltarmos para o nosso terminal,
encerrarmos o servidor da Web com Ctrl C,
reiniciarmos,
voltarmos para o navegador da Web e
recarregarmos, todas as nossas postagens terão sumido.
O que está acontecendo lá?
Agora, se observarmos o código fonte
deste módulo que o forum.py está usando,
veremos que ele não está de fato
usando um banco de dados.
Ele está simulando isso com variáveis antigas
que desaparecem quando o programa é encerrado.
Nos próximos exercícios,
você adaptará esse código para
usar um banco de dados para criar postagens persistentes.
Primeiro,
vamos dizer olá para o sistema de banco de dados
que usaremos, o PostgreSQL.
这节课接下来 你就要用一些 Python 代码
它们已经在你的 vagrant 文件夹里的 forum 子文件夹里了
随便用你喜欢的编辑器来编辑它
在你保存时
你的改动就可以从虚拟机里看到了
好的 这都是什么代码呢?
这是一个网页论坛 还不是很完善
可以用命令行像其他的 Python 程序一样运行
运行后 它会告诉你它在收听8000端口
并且你可以用浏览器 通过 http://localhost:8000 访问到它
按提示做 你就会看到这样的东西
嗯 这儿很空
我们来发几个贴子
现在我们回头看看终端
使用 Ctrl-C 终止网页服务 然后重新打开
回到浏览器然后刷新 所有的贴子都消失了
怎么会这样呢?
我们来检查一下 forum.py 正在用的这个 module 的源代码
然后发现 它根本没有用到什么数据库
只有一些旧的并且一旦退出就不存在的变量
所以接下来的几个练习里
你就要完善这段代码 让它用上数据库 把贴子存下来
首先
让我们来认识一下将要用到的数据库系统 PostgreSQL