Now popular Python ... Python for AI development and deep learning, Python for image processing, Python for log analysis, Python with Django for creating web applications ...
I felt like I heard a voice like this "Everyone is doing Python, only you who aren't doing it"
I don't know, I'll do Python.
Learn from the official Django documentation. https://docs.djangoproject.com/ja/3.0/
The official documentation is in Japanese and is (probably) easy to read as it is.
I'm currently B2. I'm not an information department.
I usually develop web applications on Elixir / Phoenix. (Phoenix is Elixir's web framework)
I've touched Python lightly in class.
So as a level, I've used other web frameworks, but I've never touched Python as well as Django. I think it will be an article for about a few people.
Also, for some reason, the installation was done on my PC (really why), so I will skip the introduction here.
By the way, if the version comes back with the following command, you can even install Django.
$ python -m django --version
This article is the first article in the series.
Summary of Django tutorials for beginners by beginners ① (project creation ~) Django tutorial summary for beginners by beginners ② (Model, Admin) Django tutorial summary for beginners by beginners ③ (View) Django tutorial summary for beginners by beginners ④ (Generic View) Django tutorial summary for beginners by beginners ⑤ (test) Django tutorial summary for beginners by beginners ⑥ (static file) Summary of Django tutorials for beginners by beginners ⑦ (Customize Admin)
https://docs.djangoproject.com/ja/3.0/intro/tutorial01/
$ django-admin startproject mysite
This will create the following
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Go from above
mysite/
-The mysite directory, which is the root directory, has no particular meaning, and you can rename it appropriately at any time.
manage.py
-Command line utility. Perform project-related operations such as starting a server.
mysite
·app name
__init__.py
An empty file that tells Python that it is a Python package
settings.py
-Project configuration file
urls.py
・ Write a URL (a router called Rails?)
asgi.py
wsgi.py
Well I do not know
$ python manage.py runserver
Go to http://127.0.0.1:8000/ If this comes out, it is a success.
Also,
$ python manage.py runserver 8080
Change the port and start
$ python manage.py runserver 0:8000
You can also specify the server ip with.
Create it with the following command. Also, it seems that you can create an application anywhere in the file.
$ python manage.py startapp polls
The contents of the created application are as follows.
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
Edit polls / views.py.
polls/view.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
It is necessary to associate the url. Create urls.py.
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
After that, it will be reflected in mysite / url.py.
mysite/url.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')), #add
path('admin/', admin.site.urls),
]
This series of flow is a little different from routing such as Rails.
If you access http: // localhost: 8000 / polls / with the settings so far
If this happens, you are successful!
path takes an argument as follows
path(route, view, kwargs=None, name=None)
Path pattern such as route
" polls / "
view
view called when the pattern matches
kwargs
Pass any keyword argument as a dictionary to the target view.
name
Give the URL a name so that it can be called from anywhere in the project.
Recommended Posts