Rails 2023 年,Rails 还有什么比 Django 显著更强的地方吗?

YaGregor · 2023年10月04日 · 最后由 Mark24 回复于 2023年10月11日 · 1238 次阅读

作为一个对 Django 比较熟的人,稍微学习过 Rails,感觉大部分和 Django 差不多?对于一般性的认知提升来说,有深入学 Rails 的价值吗?

作为一个对 Rails 比较熟的人,稍微学习过 Django,感觉比 Django 强多了。

都用过的表示仅仅 ActiveRecord::Enum 用起来就爽好多,少写不少代码

我没有用过 Django,单从文档看,Django 对前端技术不持立场,开发者根据自己的情况选择前端技术;而 Rails 创始人 DHH 强烈推荐全栈开发,默认包括一个叫 hotwire 的从网页到移动端的前端框架,当然如果不想用 hotwire 也可以选择集成其他“主流”的前端技术。

Rails 7.1 发布 Hacker News 上有一些多框架使用者的评论,可以看一下 https://news.ycombinator.com/item?id=37787130

ORM 用过都说好,用过都说香,觉得自己命都长了。 用其他的感觉就一句话,人生苦短,能短则短。

写过好多年 Rails 的说,最近也在用 Django 写业务,觉得比较舒服的地方是 Django Admin, 简单的后台管理功能,很方便 其他的没有 Rails 香,比如 ORM

前后端一体化开发就是最快的效率。

1.约定大于配置 2.立足后端但始终关注前端技术并推动融合 3.Rails 基金会支持。

yukihiro_matz 回复

还有就是 Rails 生态好

I am using Django. Let me tell you how ugly Django template is: https://docs.djangoproject.com/en/4.2/topics/templates/

For example.

{% extends 'layout_basic_table.html' %}
{% block title %}Need Further Translation Texts{% endblock title %}
{% block header_title %}Need Further Translation Texts{% endblock header_title %}
{% block table_content %}
  <thead>
  <tr>
    <th>#</th>
    {% for field in fields %}
      <th>{{ field }}</th>
    {% endfor %}
  </tr>
  </thead>

  <tbody>
  {% for item in need_translation_texts %}
    <tr>
      <td>{{ forloop.counter }}</td>
      <td>
        <a href="https://api.pagg-prod.com/admin/api/translationdictionary/{{ item.0 }}/change/">
          {{ item.0 }}
        </a>
      </td>
      <td>{{ item.1 }}</td>
    </tr>
  {% endfor %}
  </tbody>
{% endblock table_content %}

You cannot embed any Python code in Django template!

Template in Django is just for display. You can never write any Python code in template! That means if you want to make the item.1 to be lower-cased, no direct way in template to do it. You have to prepare the item.1 well in the backend controller code.

https://www.google.com/search?q=can+i+use+python+object+and+call+method+in+django+template&oq=can+I+use+python+object+and+call+method+in+&gs_lcrp=EgZjaHJvbWUqBwgBECEYoAEyBggAEEUYOTIHCAEQIRigATIHCAIQIRigATIHCAMQIRigATIHCAQQIRigATIHCAUQIRigATIKCAYQIRgWGB0YHjIHCAcQIRiPAjIHCAgQIRiPAtIBCTI0MTI4ajBqN6gCALACAA&sourceid=chrome&ie=UTF-8

So for Ruby developer, you are probably want to write code in views like "#{school.name} #{school.ranking}". This is not allowed in Django template. You have to do it in the Django views.py (like: Ruby controller), and create variables like school_name, school_ranking and use them directly in template.

So Django is awful. Web companies using Django are on the edge of bankrupt.

gazeldx 回复

Ruby 自带的 ERB 都没法 嵌套。 Rails 打了补丁可以。 比如在 Sinatra 里用裸的 ERB 就很难受。

11 楼 已删除
需要 登录 后方可回复, 如果你还没有账号请 注册新账号