Django - Mac OSX 工作流程 - 有关高效开发方法的问题
我将概述我的工作流程,并希望获得一些关于如何提高其效率的建议。现在看起来有点麻烦和重复(我讨厌这一点),所以我正在寻找一些改进。请记住,我对 django 及其工作原理仍然很陌生,但我是一个非常流利的编码器(恕我直言)。所以这里是......
工具(我每天都使用这些,所以我不想改变):
- Mac OSX Leopard
- TextMate
- Terminal w/tabs
- Perforce
Asminations
- Django Basics(教程/买了书吗)
- Python Fluent(在 IDLE 支持下运行 2.6)
- 开始我的第一个应用程序在 models.py 上工作
开始
- 创建一个 TextMate 项目,其中包含整个 django 树。
TextMate 项目 http://img.skitch.com/20090821-g48cpt38pyfwk4u95mf4gk1m7d.jpg
- < p>在终端的第一个选项卡中启动服务器
python ./manage.py runserver
在终端窗口的第二个选项卡启动 shell
python ./manage.py shell
This生成 iPython,让我开始开发工作流程
工作流程
创建并构建一个名为 models.py 的基本模型
构建一个基本模型
class P4Change(models.Model):
"""This simply expands out 'p4 describe' """
change = models.IntegerField(primary_key=True)
client = models.ForeignKey(P4Client)
user = models.ForeignKey(P4User)
files = models.ManyToManyField(P4Document)
desc = models.TextField()
status = models.CharField(max_length=128)
time = models.DateField(auto_now_add=True)
def __unicode__(self):
return str(self.change)
admin.site.register(P4Change)
- 在第一个终端(运行服务器)中停止它 ^C 并syncdb启动服务器
> python ./manage.py syncdb
Creating table perforce_p4change
Installing index for perforce.P4Change model
- 在 shell 终端窗口中加载它..
> python ./manage.py shell
Python 2.6.2 (r262:71600, Apr 23 2009, 14:22:01)
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: from perforce.models import *
In [2]: c = P4Client.objects.get_or_create("nellie")
- 它是否中断了 是/否 如果它不起作用然后这样做:
- 停止 shell
- 清除数据库
- 重建数据库
- 修复代码
- 重新加载 shell
- 重新加载模块
- 祈祷...
问题/评论/想法
- 是我的问题还是这看起来效率非常低?
- 看来我应该能够重新加载(模块),但我不知道如何做到这一点..有人吗?
- 看起来我应该能够从 TextMate 中测试这个?有人吗??
- 即使只是为了摆脱困境,我也必须确认我想离开。
这样做的目的是让你们所有的天才向我展示一种更高效的工作方式。我完全愿意接受合理的建议。我不倾向于更换工具,但我愿意接受批评。
I am going to outline my workflow and I would like some suggestions on how to improve the efficiency of this. It seems right now a bit cumbersome and repetitive (something I hate), so I am looking for some improvements. Keep in mind I'm still new to django and how it works but I'm a pretty fluent coder (IMHO). So here goes...
Tools (I use these everyday so I'm not inclined to shift):
- Mac OSX Leopard
- TextMate
- Terminal w/tabs
- Perforce
Assumptions
- Django Basics (Did the tutorials/bought the books)
- Python Fluent (running 2.6 with IDLE Support)
- Starting my first Application working on models.py
Starting out
- Create a TextMate Project with the entire django Tree inside of it.
TextMate Project http://img.skitch.com/20090821-g48cpt38pyfwk4u95mf4gk1m7d.jpg
In the first tab of the terminal start the server
python ./manage.py runserver
In the second tab of the terminal window start the shell
python ./manage.py shell
This spawns up iPython and let's me start the development workflow
Workflow
Create and build a basic Model called models.py
Build a basic Model
class P4Change(models.Model):
"""This simply expands out 'p4 describe' """
change = models.IntegerField(primary_key=True)
client = models.ForeignKey(P4Client)
user = models.ForeignKey(P4User)
files = models.ManyToManyField(P4Document)
desc = models.TextField()
status = models.CharField(max_length=128)
time = models.DateField(auto_now_add=True)
def __unicode__(self):
return str(self.change)
admin.site.register(P4Change)
- In the first terminal (Running server) stop it ^C and syncdb start server
> python ./manage.py syncdb
Creating table perforce_p4change
Installing index for perforce.P4Change model
- In the shell terminal window load it..
> python ./manage.py shell
Python 2.6.2 (r262:71600, Apr 23 2009, 14:22:01)
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: from perforce.models import *
In [2]: c = P4Client.objects.get_or_create("nellie")
- Did it break yes/no if it did not work then do this:
- Stop the shell
- Clear the database
- Rebuild the database
- Fix the code
- Reload the shell
- Reload the modules
- PRAY...
Issues / Comments / Thoughts
- Is it me or does this seem terribly inefficient?
- It seems like I should be able to do a reload(module) but I can't figure out how to do this.. Anyone?
- It would seem as though I should be able to test this from within TextMate?? Anyone??
- Even to just get out of the shell I have to verify I want to leave..
The point of this is for all of you geniuses out there to show me the light on a more productive way to work. I am completely open to reasonable suggestions. I'm not inclined to shift tools but I am open to criticisms.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢所有阅读本文并正在寻找更好方法的人。我认为单元测试绝对是更简单的方法。
因此,根据文档,您只需创建一个与 models.py 并行的文件tests.py 并将测试放入其中。
现在您可以简单地启动终端并执行 python manage.py test ,这将运行测试,但这又是一个非常有限的视图,并且仍然需要您换入/换出程序。所以这是您直接从使用 ⌘R 进行文本交互。
在顶部添加一条导入行,在底部添加几行。
现在 ⌘R 将直接从 TextMate 运行。
Thanks to everyone who read this and is looking for a better way. I think unit tests are definately the simpler approach.
So according to the docs you simply need to create a file tests.py parallel to models.py and put tests in there.
Now you can simply fire up the terminal and do python manage.py test and that will run the tests but again that's a pretty limited view and still requires you to swap in/out of programs.. So here is how you do this directly from Textmate using ⌘R.
Add an import line at the top and a few line at the bottom.
And now ⌘R will work directly from TextMate.
好吧,我会咬的:-) 这是我使用的:
MAMP。您将获得功能齐全的 Apache + MySQL + PHP + phpMyAdmin 堆栈来管理 Web 和 DB 层。它非常适合超出基本 SQLite 范围的应用程序。基本版是免费的,但我继续选择专业版,因为我经常使用它并且想支持开发人员。测试并确保一切正常的一个好方法是从 Django 测试服务器开始,然后在您自己的计算机上在 MAMP 下进行部署和测试,最后将其推送到您的部署站点。 (您可以尝试使用类似
Eclipse + PyDev + PyDev 扩展。正确配置后,您将获得 Python 代码完成、良好的开发环境和完整的调试。您可以对其进行配置,以便它为您运行 Django 测试服务器,并且您可以在 Django 源代码或您自己的代码中的任何行上设置断点。我喜欢 Eclipse 的一点是,一旦您习惯了该环境,您还可以用它进行 C/C++、Java、JavaScript、Python 和 Flex 编码。
Aptana for Eclipse。它有助于开发 AJAX 前端和编辑 Django 模板,以拥有一个像样的 Javascript + HTML 编辑器/调试器。
TextMate。我创建了一个包含所有 Django 源代码的 TextMate 项目,并将其保存在 Django 源目录中。这样我就可以通过Django源码快速进行项目搜索并单击打开源文件。您还可以对其进行设置,以便可以在 Eclipse 和 TextMate 编辑器之间来回切换,并让它们自动重新加载。
一个不错的 MySQL 或 SQLite 编辑器。 phpMySQLAdmin 还可以,但有时拥有一个独立的工具会更好。 SequelPro(以前称为 CocoaMySQL)和 Navicat 对于 MySQL 来说都非常好。优点之一是,一旦部署了应用程序,您就可以使用这些工具远程访问部署数据库服务器并从桌面对其进行调整。在 SQLite 方面 SQLiteManager 和 Base 是很好的商业工具,免费的 FireFox SQLite 管理器。至少你可以看到 Django 在幕后做了什么。
我使用 Subversion 进行版本控制主要是因为它在独立的 Mac Mini 上运行,可以将所有内容保存到 Drobo RAID 阵列,并自动将所有内容备份到其他几个外部驱动器。这是在 Time Machine 之上的(是的,我很偏执:-) 我曾经使用 Eclipse 的 SVN 支持,但现在我是 版本。在某些时候,当我能找到一个好的镜像方案时,我会切换到 Mercurial、Git 或 Bazaar,但目前来说效果很好。
终端加上一堆 shell 脚本。每个人对此都有自己的版本。当涉及到这些事情时,我非常懒,所以我设置了一堆 bash 快捷方式来帮助加快重复的 Django 管理任务。我 不久前发布了这些。
其中大部分可以免费或支付适度费用(<100 美元)获得。但如果我必须选择在 Mac 上进行 Django 开发的“必备”项目,那我会选择 Eclipse 和 PyDev。
我确信我错过了一些。很高兴听到其他人都在使用什么工具。
OK, I'll bite :-) Here's what I use:
MAMP. You get a fully functional Apache + MySQL + PHP + phpMyAdmin stack to manage the web and DB layers. It's great for apps that go beyond basic SQLite. Basic version is free but I went ahead and popped for Pro because I use it so much and wanted to support the devs. A good way to test and make sure everything works is start with the Django test server, then deploy and test under MAMP on your own machine, and finally push it out to your deployment site. (You could try to automate the process with something like Fabric).
Eclipse + PyDev + PyDev extensions. Once configured properly you get Python code completion, a nice development environment, and full debugging. You can configure it so it runs the Django test server for you and you can set breakpoints on any line in Django source or your own code. The thing I like about Eclipse is that once you get used to the environment, you can also it for C/C++, Java, JavaScript, Python, and Flex coding.
Aptana for Eclipse. It helps when developing AJAX front-ends and editing Django templates to have a decent Javascript + HTML editor/debugger.
TextMate. I've created a TextMate project that includes all of Django sources and saved it in the Django source directory. This way, I can quickly do project searches through Django source and single-click open the source file. You can also set it up so you can go back and forth between Eclipse and TextMate editors and have them auto-reload.
A decent MySQL or SQLite editor. phpMySQLAdmin is OK but sometimes it's good to have a standalone tool. SequelPro (formerly CocoaMySQL) and Navicat are all pretty good for MySQL. One advantage is that once your app is deployed, you can use these tools to remotely access the deployment DB server and tweak it from your desktop. On the SQLite side SQLiteManager and Base are good commercial tools, as is the freebie FireFox SQLite Manager. At the very least you can watch what Django's doing under the hood.
I use Subversion for version control mostly because it runs on a standalone Mac Mini which saves to a Drobo RAID array plus auto-backups everything to a couple other external drives. This on top of Time Machine (yes, I'm paranoid :-) I used to use Eclipse's SVN support but now I'm a big fan of Versions. At some point when I can figure out a good mirroring scheme I'll switch to Mercurial, Git, or Bazaar, but for now this works pretty well.
Terminal plus a bunch of shell scripts. Everyone has their own version of this. I'm pretty lazy when it comes to these things so I set up a bunch of bash shortcuts to help speed up repetitive Django admin tasks. I posted these up a while back.
Most of these can be had for free or a moderate fee (< $100). But if I had to pick the 'must have' items for Django development on the Mac, it would be Eclipse and PyDev.
I'm sure there are some I've missed. Be great to hear what tools everyone else is using.
首先,在模型就位之前无需执行
./manage.py runserver
。其次,清除数据库/重建数据库应该在修复代码后完成,并且可以通过
./manage.py reset perforce
一举完成。第三,你正在输入的内容每次 shell(导入模型,尝试创建对象)都应该在测试套件中编写。然后你可以执行
./manage.py test perforce
而不是启动 shell 并再次输入。实际上,如果您使用测试套件,则不需要,因为它每次都会创建一个干净的虚拟数据库,并在完成后为您分解它。第四,尝试“观察测试是否通过”,而不是“祈祷……”。
First of all, no need to do a
./manage.py runserver
until your models are in place.Second, clear the database/rebuild the database should be done after fixing the code, and can be done in one fell swoop with
./manage.py reset perforce
Third, the things that you are typing out in the shell each time (import models, try creating an object) should be written in a test suite instead. Then you can do
./manage.py test perforce
instead of firing up the shell and typing it again. Actually, if you're using the test suite, you won't need to, because it will create a clean dummy db each time, and break it down for you when it's done.Fourth, Instead of "PRAY...", try "Watch tests pass."
我发现更频繁地编写 单元测试 更顺畅,并且只使用 shell当某件事发生故障并且原因不明并且您想要四处寻找并找出原因时。一开始它效率有点低,但很快就变成了一种美妙的工作方式。
在处理视图并需要运行服务器之前,我还倾向于集中精力使模型或多或少稳定和完整(至少就影响表结构而言)。这往往会预先加载尽可能多的重置,因此您可以在价格便宜的时候进行重置。
I find it smoother to write unit tests more often and only use the shell when something is failing and it's not obvious why and you want to poke around to figure it out. It is a little more inefficient at the very beginning, but quickly becomes a wonderful way to work.
I also tend to concentrate on getting the model more or less stable and complete (at least as far as what will affect table structure) before I work on the views and need to run the server. That tends to front-load as many resets as possible so you're doing them when it's cheap.