Try running python in a Django environment created with pipenv

Overview

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.

reference

environment

MacOS 10.15 pipenv version 2018.11.26 Django 2.2.6 VSCode 1.39.2

procedure

Create a Django project

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
into-pipenv-shell-m.png

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.

Install the extension to work with Python in VS Code.

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.

  1. Open Extensions from the menu on the left side of VS Code (it looks like four blocks lined up)
  2. Type Python in the Search Extensions in Marketplace
  3. A list of extensions related to Python will be displayed. Select the extension called Python whose author is Microsoft and press the green Install button.
VSCode-search-python-extensions-m.png

Set VS Code to refer to pipenv's python

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
check-python-path-m.png 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. set-path-setting-jason-m.png

Confirm that Python of the set path is displayed in the blue part at the bottom left of VS Code. after-setting-setting-json.png

Try starting Django's server

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: runserver-error.png

Start the Django server. Execute the following command.

$ python manage.py runserver
runserver-success.png

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.

server-running-m.png

Create a Django app

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.

Create sample code

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.

Operation check

Save the edited program and run the server.

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.

program-run-result-m.png

Next time, I'll implement a simple web API in Django [https://qiita.com/Ajyarimochi/items/ce072d91a2c00cd0d861)

Recommended Posts

Try running python in a Django environment created with pipenv
Start Django in a virtual environment with Pipenv
Install Django in a pipenv virtual environment
Create a virtual environment with conda in Python
[Python] Build a Django development environment with Docker
Build a Django environment with Vagrant in 5 minutes
Work in a virtual environment with Python virtualenv.
A simple to-do list created with Python + Django
Quickly build a Python Django environment with IntelliJ
Try running Python with Try Jupyter
Try embedding Python in a C ++ program with pybind11
[Django3] Display a web page in Django3 + WSL + Python virtual environment
Create a virtual environment with Python!
Building a virtual environment with Python 3
[SAP CP] Web API created with python in CF environment
pytorch @ python3.8 environment construction with pipenv
Try running a function written in Python using Fn Project
Try to create a python environment with Visual Studio Code & WSL
[Pyenv] Building a python environment with ubuntu 16.04
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try logging in to qiita with Python
Building a Python3 environment with Amazon Linux2
Try working with binary data in Python
Try sending a SYN packet in Python
Try drawing a simple animation in Python
CSS environment created in 10 minutes using Django
[Python] Create a virtual environment with Anaconda
Building a Python 3.6 environment with Windows + PowerShell
Created a darts trip with python (news)
Try HTML scraping with a Python library
Build a python virtual environment with pyenv
Try a functional programming pipe in Python
Try drawing a map with python + cartopy 0.18.0
I created a password tool in Python.
Build a modern Python environment with Neovim
Introduced sip-4.14 in python3.2.2 environment with MacOS 10.7.4
Get a list of packages installed in your current environment with python
[Django] sqlite version error when running python manage.py in aws cloud9 environment
Create a Vim + Python test environment in 1 minute
[Python] Get the files in a folder with Python
Project cannot be created with Python3.5 (Windows) + django1.7.1
Try to calculate a statistical problem in Python
Ubuntu18.04.05 Creating a python virtual environment in LTS
Building a python environment with virtualenv and direnv
Try running Google Chrome with Python and Selenium
Build a python environment with ansible on centos6
Building a Python environment with WLS2 + Anaconda + PyCharm
Try to draw a life curve with python
Create a python3 build environment with Sublime Text3
Try to make a "cryptanalysis" cipher with Python
Create Nginx + uWSGI + Python (Django) environment with docker
Try installing GeoSpark (Apache Sedona) in Python environment
A memo when creating a python environment with miniconda
Build a Django development environment with Doker Toolbox
Try working with Mongo in Python on Mac
Think about building a Python 3 environment in a Mac environment
Commands for creating a python3 environment with virtualenv
Create a new page in confluence with Python
Try to make a dihedral group with Python
Use Python in Anaconda environment with VS Code