DEV Community

Adarsh
Adarsh

Posted on

Creating a "Hello, World!" Application with Django and Python

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It's built by experienced developers and takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. In this article, we'll walk through the process of creating a simple "Hello, World!" application using Django.

Step 1: Install Django

Before we start, ensure you have Python and pip (Python's package installer) installed on your system. If not, you can download Python from the official site and pip will be installed along with it.

Once Python and pip are installed, you can install Django using the following command in your terminal:

pip install django

Create a New Django Project

After installing Django, the next step is to create a new Django project. This is done using the following command:

django-admin startproject myproject

Replace "myproject" with the name you want for your project. This command will create a new directory with the same name as your project, containing the necessary files for a Django project.

Step 3: Create a New Django Application

In Django, a project can consist of multiple applications. Let's create a new application within our project using the following command:

python manage.py startapp myapp

Replace "myapp" with the name you want for your application.

Step 4: Write the "Hello, World!" View

In Django, a view is a type of web response. A simple "Hello, World!" view can be created by editing the views.py file in your application directory. Replace its content with the following:

`from django.http import HttpResponse

def hello(request):
return HttpResponse("Hello, World!")`

Step 5: Map the View to a URL

Next, we need to map the view to a URL. This is done by editing the urls.py file in your project directory. Replace its content with the following:

```from django.urls import path
from myapp.views import hello

urlpatterns = [
path('', hello),
]```

Step 6: Run the Server

Finally, you can run the Django development server to see your "Hello, World!" application. Use the following command:

python manage.py runserver

Now, if you navigate to http://127.0.0.1:8000/ in your web browser, you should see your "Hello, World!" message.

Conclusion

In this article, we've walked through the process of creating a simple "Hello, World!" application using Django and Python. This is just the beginning of what you can do with Django. With its robust set of features and extensive documentation, Django is a powerful tool for web development. Whether you're building a small personal project or a large-scale web application, Django provides the infrastructure you need to develop quickly and efficiently.

Top comments (0)