[JAVA] Until Python [Django] de Web service is released [Tutorial, Part 1]

Execution environment

★ Local environment MacBook Pro (13-inch, Mid 2012) Processor: 2.5 GHz Intel Core i5 Memory: 4 GB 1600 MHz DDR3 ★ Language to be used Python Django JavaScript


Installation method

About installation, here introduction.


tutorial

Part1

After the installation is completed, in the terminal, perform from application template creation to server startup in the following order.

First, let's follow the tutorial to create a Django template. This time, I chose "mysite" because it matches the tutorial, but there is no problem if I match it with my project name. The final form of this tutorial is a voting app. It is a form that you can learn the basic parts such as template creation, url control, cgi creation, etc.

Point 1

Enter the following in the terminal.

Terminal


#Create mysite."mysite"Can be the project name.
#If you change it"mysite"Please read the part with the project name and proceed.
$ django-admin.py startproject mysite

#Use the cd command to go to mysite.
$ cd mysite

# manage.Create an app infrastructure using py.
$ python manage.py startapp polls

Here, for reference, expand the "polls" tree.

polls[tree]


polls
  ├── __init__.py
  ├── admin.py
  ├── apps.py
  ├── migrations
  │   └── __init__.py
  ├── models.py
  ├── tests.py
  ├── urls.py  <add to&Edit
  └── views.py <Edit

First, edit views.py in polls. This is the usual "Hello, world."

polls/views.py


from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

Point 2

Next, create a new "urls.py" in polls and add it. This is the basis for use in the "def index (request):" declared in views.py. Since urls are managed by regular expressions, if an incorrect url is entered in terms of security, it will be treated as an error.

By the way, views.py alone works by specifying the URL destination, but that can be difficult to manage. "Views.py" and "urls.py" are always linked, and the shape that should move is good.

polls/urls.py


from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

Next is the current tree of mysite. This time, open "urls.py" in "mysite" and edit it.

mysite/polls[tree]


├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py <this
│   └── wsgi.py
└── polls
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── models.py
    ├── tests.py
    ├── urls.py
    └── views.py

Point 3

Add "url (r'^ polls /', include ('polls.urls'))," to "url patterns" in "urls.py". At this time, if include is "include (admin.site.urls)", your version may be out of date. In that case, it is useless to refer to this tutorial. You need to do the tutorial for the old version or switch to the new version.

mysite/urls.py


from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

Return to the terminal and start the server. In addition, "python manage.py runserver 0.0.0.0:8000" is set so that it can be confirmed even from an external connection (inside the local). After startup, you can connect to [http: // localhost: 8000 / polls /](http: // localhost: 8000 / polls /) from an external terminal. Make sure your browser says "Hello, world. You're at the polls index."

Terminal


#Server startup
$ python manage.py runserver 0.0.0.0:8000
> http://localhost:8000/polls/
> Quit the server with CONTROL-C.

This is the explanation of Part 1. Part 2 includes creating a DB and so on.


Review of points.

□ Point 1 Create polls in mysite and edit wiews.py in polls.

□ Point 2 Create and edit urls.py in the polls you created earlier.

□ Point 3 Also edit the urls.py in mysite. However, there is something I want you to be careful about at this time. If include is "include (admin.site.urls)", your version may be out of date. In that case, please refer to the new version or the tutorial of the old version.

□ Bonus point Also, if the notation in "settings.py" is enclosed in "()" instead of "[]", it may be out of date. Details will be explained in the next tutorial with detailed code.


Recommended Posts

Until Python [Django] de Web service is released [Tutorial, Part 1]
Until Python [Django] de Web service is released [Environment construction]
Python Django Tutorial (5)
Python Django Tutorial (2)
Python Django Tutorial (8)
Python Django Tutorial (6)
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python Django Tutorial (3)
Python Django Tutorial (4)
Python Django tutorial summary
Django python web framework
Web application made with Python3.4 + Django (Part.1 Environment construction)
Python Django Tutorial Cheat Sheet
Until you publish a web service on GCP while studying JQuery and Python