Now that we have created an initalized a Django project. It is time to initialize a django app. An app in django is a web application that performs some operation. The project creation is just an initialized of the project with configurations for a website.
Completion of the previous tutorial – Simpe Django Project
1. Navigate to you project directory and install “pip install django”
2. Create a Django App “webapp” by executing below command on the terminal
“python manage.py startapp webapp”
Add the app to the projects settings.py under the “INSTALLED_APPS” array.
3. Navigate to views.py under the newly created app “webapp”
4. In the views.py is a file where we create functions that we want to respond to an http request. Lets create a function index as displayed below. The index function has parameter request, which will be supplied by django. We will also import the HttpResponse class from django.http so that we can use it to respond to the http requesting client. We have returned a message “Welcome to myapp” in the response.
5. Now create a new file “urls.py” file in the same directory and add the import “from . import views”. Here we are importing the functions we have created in views.py. Now add a new array urlPatterns as shown in below image with entries as mentioned. Here we telling django the mapping between request url and the corresponding function.
6. We have created the mapping in the webapp app but now we need to tell the project about the mappings. Navigate to the project “urls.py” file as shown below. Add a new path in the “urlpatterns” and import “include” as shown.
Here on the line 21 we have told django, whenever request is for a url with pattern “/api” it should look for the mapping in the urlpatterns mentioned in the webapp app.
7. Now run the server with “python3 manage.py runserver”. Run the url http://127.0.01:8000/api . Similarly we can add multiple paths to the webapp application. We will more about it in the next tutorial