The following article explains how to Create a Simple Django App.
- Navigate to the root directory of your Django project in the command prompt or terminal.
- Create a new app named “myapp” as given below.
python manage.py startapp myapp
- So, you will get “myapp” in your project directory, with the following file structure.
myapp/
migrations/
__init__.py
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
- Open the “views.py” file in your text editor and add the following code.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world!")
This creates a simple view that returns a “Hello, world!” message when the app is accessed.
- Open the “urls.py” file in the “myapp” directory and add the following code.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
This maps the root URL of the app to the “index” view that you just created.
- Finally, open the “urls.py” file in your project directory and add the following code to include the URLs of your “myapp” app.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('myapp/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
This includes the URLs of your “myapp” app under the “/myapp” URL.
- Now you can run the development server using the following command.
python manage.py runserver
- Open your web browser and go to “http://localhost:8000/myapp/“. The “Hello, world!” message should appear that you defined in your view.
Congratulations, you have created a simple Django app!
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP