Web development beginner. A personal note after reading the tutorial.
Django
Pronounced django.
Supports python3.6 or later.
One of the python web frameworks
What is a web framework? A template that helps you implement a web service. To make it easier to create web services It provides various functions on the web framework side. In the case of Django, it seems that you can easily write exchanges with the DB. However, restrictions: Hierarchy and file name rules are defined, and it will not work unless it is properly followed.
WSGI(web server gateway interface) Interface for connecting a web application and a web server Use WSGI instead of Apache. Now run Django on your web server. Gunicorn is used in this environment
Query statements are mostly hidden in Django Define data type in model instead Besides, PostgreSQL, MySQL, Oracle can be used Should I use this to make the DB redundant? SQLite is used in this environment
$ django-admin startproject mysite #step1:Project creation
$ cd mysite ##Once you're in the django project you created, manage.There should be py. Move to that dir. Basic operation is manege.Conducted with py
$ python manage.py startapp polls #polls is the server name, diverted from the official#step2:Development server creation
Here is an example of the official Document.
Specify with the path function in the'url_pattern' list in url.
polls/url.py
path('<int:question_id>/', views.huga, name='detail') #The character string specified here can also be used in html as the name of the url.
This can be expressed in the html file as follows.
<li><a href="{% url 'detail' question_id %}">{{ question.question_text }}</a></li>
Actually, there are multiple apps in one project, so first divide the namespace there so that it is unique as a whole.
polls/url.py
appname = 'polls'
urlpatterns = [
path('<int:question_id>/', views.huga, name='detail'),
]
<li><a href="{% url 'polls:detail' question_id %}">{{ question.question_text }}</a></li>
$ python manage.py runsever #Start the development server. Since the production server is started by WSGI, it will be a different command
$ python manage.py migrate #INSTALLED_See APPS & settings.Create database table according to DB setting of py file
$ python manage.py makemigrations polls #Reflect model changes in DB
$ python manage.py createsuperuser #Create a user for the admin page
polls/views.py
from django.http import HttpResponse
from django.template import loader
from .models import Question
def index(request):
latest_question_list = Question.object.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {'latest_question_list':latest_question_list}
return HttpResponse(template.render(context, request))
HTTP response can be written using render function
polls/views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.object.order_by('-pub_date')[:5]
context = {'latest_question_list':latest_question_list}
return render(request, 'polls/index.html' , context)
Once here. Continue from general-purpose view
Recommended Posts