dedtech.info

Information about computer technology.

Create A Simple Django App With Python

This blog post will explain how to create a simple Django app with Python. The server will take a request and return a response.

Create a django project named myproject.

django-admin startproject myproject

Navigate to the myproject directory and run this command to create an app named hello.

python manage.py startapp hello

In the file myproject/hello/views.py, add the code below.

from django.shortcuts import render
from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello world!")

In the file myproject/hello/urls.py, add the code below.

from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.hello, name='hello'),
]

In the file myproject/myproject/urls.py, add the code below.

from django.urls import include, path

urlpatterns = [
    path('', include('hello.urls'))
]

Navigate to the myproject directory and run this command to start the server.

python manage.py runserver

Run this command in a web browser.

http://127.0.0.1:8000/hello/

Leave a Reply

Your email address will not be published. Required fields are marked *