Django 有足够的脚手架吗? (à Ruby on Rails)

发布于 2024-10-25 22:05:13 字数 126 浏览 2 评论 0原文

Django 是否有足够的脚手架

可能是新发布的1.3版本里有,不过我还没找到。

Is there any adequate scaffolding for Django?

It may be in the newly released 1.3 version, but I haven't found it yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

禾厶谷欠 2024-11-01 22:05:13

我已经查看过,但尚未找到 Django 的类似 Rails 生成命令的东西。 Django 有一些不同的哲学。它为您提供了使事情变得轻松的工具,但实际上并没有为您做这件事(管理界面除外)。从总体上看,我认为这是可以的。当我使用 Rails 的脚手架时,我通常无法保留很多自动生成的东西。当我这样做时,django 管理界面可能也会起作用并为我提供更多功能。

相反,我建议阅读 Django 教程步骤 4,其中介绍了通用视图,然后Django 书的第 7 章 介绍了表单。你必须对第七章有耐心,因为作者认为在他们教你简单的方法之前,你想知道困难方法的细节。 (尝试在页面中搜索短语 django.forms)

最后,rails 和 django 之间要做的工作量是相同的,而 Django 可能会稍微少一些。然而,您没有一个命令可以自动为您提供样板代码以用作基础。

I've looked and not yet found something for Django quite like the Rails Generate command. Django has a bit of a different philosophy. It gives you tools to make doing things easily but doesn't actually do it for you (except the admin interface). In the grand scheme of things, I think this is OK. When I use rails' scaffolding I'm not able to often keep much of the auto-generated stuff. When I do, the django admin interface would probably also have worked and given me more functionality.

Instead, what I suggest is reading through the Django tutorial step 4, which introduces generic views, and then chapter 7 of the Django book which introduces forms. You have to be patient on chapter 7 because the authors think you want to know the minute details of the hard-way before they teach you the easy way. (try searching the page for the phrase django.forms)

In the end the amount of work you have to do between rails and django is equivalent, and maybe slightly less with Django. However you don't have one command to automatically give you boilerplate code to use as a foundation.

瘫痪情歌 2024-11-01 22:05:13

所以Django 1.3仍然缺乏“脚手架”功能。不好。
脚手架的最佳之处在于,它允许开发人员立即开始项目,而无需回忆所有“模型”、“url”和“视图”语法。

看这个例子,让我们开始新的项目和应用程序:

$django-admin startproject mysite
$python manage.py startapp blog

现在我们需要从几乎空的文件中手动“启动”所有内容。
但是用这种方式来做会非常方便(就像在 Rails 中一样)

$python manage.py scaffold app:blog model:Post title:string content:text 

这应该给我们:
models.pyviews.py

class Post(models.Model):
    title    = models.CharField
    content  = models.TextField

def index(request):
    posts = Post.objects.all().order_by('-id')
    return render_to_response('blog/index.html', {'posts': posts})

并以某种方式更新 urls.py,...或者不是,这更复杂但不太需要

在未来的 Django 版本中实现这一点应该不难。如果我有足够的 Django 知识和经验,我就会这样做。不幸的是,我没有做很多 Django 项目,这就是我需要这个功能的原因。

So Django 1.3 still lacks 'scaffold' functionality. Not good.
What is best in scaffold, is that it allows developer to immediately start with the project, without recalling all 'models', 'urls' and 'views' syntaxes.

Look at this example, let's start new project and app:

$django-admin startproject mysite
$python manage.py startapp blog

and now we need to manually 'START' everything, from almost empty files.
BUT it would be very convenient to do it in this way (like in rails)

$python manage.py scaffold app:blog model:Post title:string content:text 

This should give us:
models.py

class Post(models.Model):
    title    = models.CharField
    content  = models.TextField

views.py

def index(request):
    posts = Post.objects.all().order_by('-id')
    return render_to_response('blog/index.html', {'posts': posts})

and update urls.py somehow, ... or not, this is more complicated but less needed.

This should not be difficult to implement in future Django releases. I would do this if I had enough knowledge and experience in Django. Unfortunately I'm not doing many Django projects and that's why I need this functionality.

梦纸 2024-11-01 22:05:13

这个更接近于类似 Rails 的脚手架: https://github.com/modocache/django-generate-scaffold

This one is closer to rails-like scaffolding: https://github.com/modocache/django-generate-scaffold

离去的眼神 2024-11-01 22:05:13

实际上有一个新的 django 包,它可以在您的应用程序模型、视图、url、测试等中构建您所需的一切 https://github.com/Abdenasser/dr_scaffold

there is actually a new django package that scaffolds everything you'd need in your application models, views, urls, tests and a lot more here https://github.com/Abdenasser/dr_scaffold

梦初启 2024-11-01 22:05:13

我发现了这个: https://github.com/madhusudancs/django-groundwork

看起来是这样正是您正在寻找的。希望有帮助。

I found this: https://github.com/madhusudancs/django-groundwork

Looks like it is exactly what you're looking for. Hope it helps.

Oo萌小芽oO 2024-11-01 22:05:13

我刚刚使用了 Django Common 提供的脚手架助手/管理命令,它似乎已经设置了一个相当大的样板代码块。选择有限,但足够好。

我浏览了一下代码,大部分看起来都很好。不过,一旦脚手架“竖立”起来,我就需要做一些清理工作:

  • 为创建的每个模型添加单独的 import 行。合并了他们。
  • 模板仍然带有旧的 (1.4) url 模板标签规范。修改它们以反映新的 (1.5) 规范,即在为每个模型创建的每个 html 文件中将第二个参数括在单引号中。
  • 使用 app.urls 模块的 include 更新了主 urls.py
  • 我使用非标准的 settings.py 设置 - 三个单独的文件 common.pydev.pyprod.py 进行设置。必须手动将应用程序添加到已安装的应用程序中。 YMMV。

(如果我想到其他任何事情,将编辑此列表)

话虽这么说,看看我不必编写的样板代码量,我想说它做得非常好!

截至目前,该存储库似乎维护得相当好 - 最后一次提交是在撰写此回复时的 18 天前。我可能会在不久的将来提交一个拉取请求/提出有关我在他们的存储库中遇到的问题的问题。

I just used the scaffold helper/management command provided by Django Common and it seems to have set up a decent chunk of boilerplate code. The options are limited, but decent enough.

I skimmed through the code and most of it looks fine. I needed to do a little bit of cleaning up, once the scaffolding was 'erected', though:

  • Separate import lines were added for each model created. Merged them.
  • The templates still carried the old (1.4) url template tag specs. Modified them to reflect the new (1.5) specs, i.e. enclosed the second parameter in single quotes, in each of the html files created, for each of the models.
  • Updated the main urls.py with an include for the app.urls module.
  • I use a non-standard settings.py setup - three separate files common.py, dev.py and prod.py for my setup. Had to add the app manually to the installed apps. YMMV.

(Will edit this list if I think of anything else)

That being said, looking at the amount of boilerplate code that I did NOT have to write, I'd say it does a very good job!

As of now, the repo seems pretty well-maintained - the last commit was 18 days ago at the time of writing this response. I'll probably submit a pull request/raise an issue about the problems I faced on their repo, some time soon.

靖瑶 2024-11-01 22:05:13

您可以检查 django-addview。它的目的是完成使用漂亮的 ncurses GUI 自动添加新视图所需的无聊、平凡的步骤。它为您做什么:

  • 扩展 CBV 或编写函数
  • 填充 CBV 的参数
  • 在给定位置创建模板
  • 为您编辑 urls.py
  • 关心所有导入

完全披露:我写的。

You may check django-addview. It is meant to do boring, mundane steps needed to add new view automatically with nice ncurses GUI. What it does for you:

  • Extend CBV or write function
  • Fill parameters of CBV
  • Creates template in given location
  • Edits urls.py for you
  • Cares about all imports

Full disclosure: I wrote it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文