Django Example:
- Install Django (if not already installed):
pip install django
- Create a new Django project:
django-admin startproject hello_django
- Change directory to the newly created project:
cd hello_django
- Create a new Django app:
python manage.py startapp hello_app
- Open
hello_app/views.py
and add the following code:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello, Django!")
- Open
hello_django/urls.py
and add the following code:
from django.urls import path
from hello_app import views
urlpatterns = [
path('', views.hello, name='hello'),
]
- Run the Django development server:
python manage.py runserver
- Open your web browser and navigate to
http://localhost:8000
to see the “Hello, Django!” message.
Ruby on Rails Example:
- Install Ruby on Rails (if not already installed):
gem install rails
- Create a new Rails application:
rails new hello_rails
- Change directory to the newly created application:
cd hello_rails
- Generate a new controller and view:
rails generate controller welcome hello
- Open
app/controllers/welcome_controller.rb
and add the following code:
class WelcomeController < ApplicationController
def hello
end
end
- Open
app/views/welcome/hello.html.erb
and add the following code:
<h1>Hello, Rails!</h1>
- Open
config/routes.rb
and add the following code:
Rails.application.routes.draw do
get 'welcome/hello'
root 'welcome#hello'
end
- Run the Rails server:
rails server
- Open your web browser and navigate to
http://localhost:3000
to see the “Hello, Rails!” message.
These examples demonstrate the basic setup and structure of a “Hello, World!” application using Django and Ruby on Rails. You can further explore these frameworks to build more complex web applications.
Leave a Reply