This article walks you through the steps of a beginner developing a coupon distribution service for the iPhone with a RESTful API and swift. It is a very detour implementation because it was implemented while examining the technical elements one by one.
Let's run the Django program in the virtual environment built with Install Django in the pipenv virtual environment. The editor uses VS Code.
MacOS 10.15 pipenv version 2018.11.26 Django 2.2.6 VSCode 1.39.2
In the directory where the virtual environment (project) of pipenv is created, execute the following command to enter the shell (chell) of pipenv.
$ pipenv shell
While in the pipenv shell, execute the following command to create a Django project.
$ django-admin startproject [Project name]
If you run the command and nothing is displayed, it is probably successful. When you run the ls command, you should have a directory with the project name you created. In my case, I named the project "ami_coupon_api", so a directory with the same name was created.
If you have VS Code installed, you probably don't have any extensions to work with Python. At a minimum, follow the steps below to install the Python extension.
If you don't do this, VS Code will not be able to load Django on the virtual environment built with pipenv and you will get an error message.
First, while inside the pipenv shell, check the python path of the pipenv virtual environment.
$ which python
Make a copy of the confirmed path.
Then open VS Code and open the directory for the Django project you just created. Then there is a directory called ".vscode" under it, so open settings.json under that directory.
Set the python path of the pipenv virtual environment you found earlier in "python.pythonPath" in Settings.json.
Confirm that Python of the set path is displayed in the blue part at the bottom left of VS Code.
While in the pipenv shell, move to the directory of the Django project you created earlier, and use the ls command to confirm that manage.py exists.
For the first time, you need to migrate manage.py before starting the server. Execute the following command to migrate manage.py.
$ python manage.py migrate
If you try to start the server before migrating, you will get this result:
Start the Django server. Execute the following command.
$ python manage.py runserver
In the browser
http://127.0.0.1:8000/
(localhost port 8000)
Try to access. If the rocket animation is displayed, the server has started successfully.
To write and run Django code, you need to create a Django app. The directory hierarchy is as follows.
To create a Django app, go to the directory where manage.py is stored (Django's project directory) while in the pipenv shell, and execute the following command.
$ python manage.py startapp [app name]
In my case, I created an app called hello. When you run the ls command in the Django project directory, a directory with the created app name is created.
Let's write a code that displays "Hello! Python & Django" in the browser.
When you open the Django project directory with VS Code, there is a directory of the application name (Hello in this case) under it, so open it. Then, there is a python file called views.py under it, so I will write the sample code there.
I edited views.py and wrote the sample code below.
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello! Python & Django!")
This is a program that returns the string "Hello! Python & Django!" To the browser in Http format when the browser accesses (requests) the function called index in views.py.
From here, make settings to access (request) views.py of the hello app from a browser or the like. Specifically, you can request views.py with the URL http://127.0.0.1:8000/hello
with hello added to the URL of the Django server you tried earlier.
First, open urls.py under the project name directory and
Add the code from django.urls import path, include
andpath ('hello /', include ('hello.urls'))
.
The added urls.py is as follows.
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include('hello.urls'))
]
This is set to look at urls.py in the hello folder when hello is specified in the URL of the request from the browser. When the server side receives the URL of the request, the first thing to look at is urls.py under the project name directory, so make such settings.
But the urls.py doesn't exist in the hello folder. Therefore, create a file called urls.py yourself under the Hello app directory and write the following code.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
This is set to pass the request to a function called index when a request comes to urls.py in the hello folder.
With these settings, if you make a request to http://127.0.0.1:8000/hello
, the request will be passed to views.py in the hello folder. It is an image that guides you by urls.py of each level.
Save the edited program and run the server.
$ python manage.py runserver
in the directory where manage.py is located *After confirming that the server has started, access http://127.0.0.1:8000/hello/
with a browser.
If you see "Hello! Python & Django!", You're successful.
Next time, I'll implement a simple web API in Django [https://qiita.com/Ajyarimochi/items/ce072d91a2c00cd0d861)
Recommended Posts