[PYTHON] Django Summary

Django Summary Nowadays

Web development beginner. A personal note after reading the tutorial.

Django

Pronounced django.

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

Django standard DB: sqlite3

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

How to use

$ 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

Folder structure

Here is an example of the official Document.

url definition

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>

django server operation

$ 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

HTTP response creation

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

Django Summary
Django Summary
Django filter summary
Django
[Learning memo] Django command summary
Django note 4
Python Summary
samba summary
Django memorandum
Django installation
Django test
python-pptx summary
Django # 2 (template)
Linux Summary
Python summary
Django Note 5
Django hands-on
Touch django
django notes
pyenv summary
String summary 1
Django basics
Django Shoho
Django defaults
Django Girls Tutorial Summary First Half
Django + Docker
Django static file (static) related settings summary
Django Glossary
pytest summary
matplotlib summary
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField
Summary of frequently used commands of django (beginner)
Django tutorial summary for beginners by beginners ③ (View)
Django tutorial summary for beginners by beginners ⑤ (test)
Django environment construction
Django ~ settings.py edition ~
Django tutorial summary for beginners by beginners ⑦ (Customize Admin)
Django tutorial summary for beginners by beginners ⑥ (static file)
AtCoderBeginnerContest180 Review & Summary
Kaggle Summary: Outbrain # 2
Django HTML Template # 2
Django Contact Form 2
LINQ library summary
Sphinx setup summary
Django begins part 1
Django model: ManyToManyField
What is Django? .. ..
AtCoderBeginnerContest181 Review & Summary
Digital technology summary
Models in Django
Python Django Tutorial (5)
Django Learning Memo
Python Django Tutorial (2)