django views map to rais controllers
polls/views.py¶
from django.http import HttpResponse
from .models import Question
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
<% if @latest_questions_list.each do |question| %>
<li> <%= link_to question.name, question %> </li>
<% else %>
<p> No polls are available </p>
<% end %>
This is django:
Using angle brackets “captures” part of the URL and sends it as a keyword argument eg question_id=34
urlpatterns = [
- ex: /polls/
path('', views.index, name='index'),
- ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
- ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
- ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
is similar to this in rails
Rails.application.routes.draw do
- dynamic segment
- path of /photos/1/2 will be dispatched to the show action of the PhotosController
- params[:id] will be "1", and params[:user_id] will be "2".
get 'photos/:id/:user_id', to: 'photos#show'
- incoming path of /photos/1?user_id=2
- params will be { controller: 'photos', action: 'show', id: '1', user_id: '2' }
get 'photos/:id', to: 'photos#show'
resources :polls do
- if only one member route use:
get 'results', on: :member
- multiple meber routes example
members do
- polls/5/results
get :results
- /polls/5/vote
get :vote
end
- /polls/search
collection do
get 'search'
end
end
end