I'm a beginner for 4 months since I started programming! I'm studying Ruby on rails and Django, so As an output (memorial note), I would like to write the difference in the basic code! !! : pencil2 :: beginner: Since I started learning from rails From a basic rails perspective, what about Django? It is written from the perspective of: smile: We hope you find it helpful. (Since I am studying, I plan to add and correct it as needed!)
It's confusing because it's covered by the name "View", but the general flow is the same. Here is my image.
(For Rails) I will describe it in routes.rb. You can check the URL pattern and prefix by hitting "rake routes" on the terminal!
config/routes.rb
root 'products#index'
get 'index', to: 'products#index'
(For Django) I will describe it in urls.py. It uses the path () function, which takes four arguments: route, view, name and kwargs. Here, the index function of view is associated with the root URL ('') so that it can be called from another file (template, etc.) with the name index. Use "as_view" to call a generic class in view.
app/urls.py
from django.urls import path
from . import views
app_name = "app name"
urlpatterns = [
path("", views.index, name="index"),
path("products/", views.ProductListView.as_view(), name="products_list"),
]
(For Rails) Since there is a rule that the view with the same name as the action name of the controller is called, No special code is required, and by preparing a view file with the same name as the action name, that view (html) file will be called.
controllers/products_controller.rb
def index
end
Ruby:views/index.html.erb
.title
I will describe html with haml etc.
(For Django) Depending on whether you use a function or a class Use the render or template_name methods, respectively (there seem to be many others)
app/views.py
#When using a function
def index(request):
return render(request, "index.html")
#When using a class
class ModelListView(ListView):
model = Model
template_name = "index.html"
(For Rails) In the html file, add @ to define the instance variable to use the variable.
products_controller.rb
def index
@product = Product.where(id: 1..10)
end
(For Django) It is shocking that data can be handled in an html file even though there are no variables!
views.rb
class XxxListView(LoginRequiredMixin, ListView):
model = Xxx
template_name = "xxx.html"
#In template, xxx_Data can be used as an array in list
def get_context_data(self, **kwargs):
context["hennsu"] = "value"
return context
#With this method"You can pass a variable called hennsu
(However, you can define variables and select data with get_context_data and get_queryset)
(For Rails) Use the render method.
Ruby:index.html.erb
<%= render '(Folder name/)yyy.html.erb' %>
-#render destination'_yyy.html.erb'I will call it
(For Django)
base.html
{% block content %}
#Insert another html file here
{% endblock %}
index.html
{% extends "base.html" %}
{% block content %}
#Base this part.Insert into html (maybe vice versa)
{% endblock %}
I think rails has a higher degree of freedom here.
(For Rails)
Ruby:view.html.haml
- if user_signed_in?
%div
current_user.name
(For Django)
template.html
{% if user.is_authenticated %}
<div> User Name: {{ user.username }} </div>
(For Rails) Creating a model (rails g model Xxx) or creating a migration file (rails g migration) -> Fill in the migration file -> Run migration (rake db: migrate)
Migration file
##It is an example
t.string :name, null: false
(For Django) Fill in models.py -> Create migration files (python manage.py make migrations) -> Run migration (python manage.py migrate)
models.py
class Xxx(models.Model):
xxx = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
I think it's because I've been touching it for a long time, Rails has simpler file relationships, The logic can be written in the erb file, and the degree of freedom of the code is high, so I feel that it is easy to create the application as intended. Django, on the other hand, feels a bit quirky in composition. However, the amount of description is small, and it seems to be fast once you get used to it. In addition, I think the advantage of python lies in the rich library, so I feel that the point is whether or not the abundant library can be utilized in the application. (I will study more and fix it at any time!) Thank you for reading: bow_tone3:
Recommended Posts