Django tutorial 2.
このページには、第2部から第4部までの内容が含まれています.
最初の設定pyをチェックして
データベースのデフォルトはdefault sqliteです.
他のデータベースを使用する場合
ENGINE : 'django.db.backends.sqlite3', 'django.db.backends.postgresql',
'django.db.backends.mysql',
'django.db.backends.oracle'
上の値で修正して適用します.
TIME_ZONE
この図のTIME ZONE='UTC.
私たちは韓国人なので、asia/soulに変えましょう.
INSTALLED_APPS
前のtutorial 1では、インストールされているappsを見てみましょう.
デフォルトでは、INSTALLED APPSには、Djangoに付属する次のアプリケーションが含まれています.
django.contrib.admin–管理用のサイト.すぐに使えます.
django.contrib.auth–認証システム.
django.contrib.contenttypes–コンテンツタイプのフレームワークに使用します.
django.contrib.セッション–セッションフレームワーク.
django.contrib.Messages–メッセージングフレームワーク.
django.contrib.staticfiles–静的ファイルのフレームワークを管理します.
views?
-django特定の機能とテンプレートを提供するWebページ
たとえば、エンベロープは次のとおりです.ホームページ 詳細ディレクトリページ 日付ページ コメント機能等
同じビューページを持つことができます. 以下の4ページを作成してみます.
新規ビューコンテンツの作成
viewsを作成した場合はurls
に接続します
正式文書の説明
When somebody requests a page from your website – say, “/polls/34/”, Django will load the mysite.urls Python module because it’s pointed to by the ROOT_URLCONF setting.
->ブラウザが「/policy/34/」を要求した場合
It finds the variable named urlpatterns and traverses the patterns in order. After finding the match at 'polls/', it strips off the matching text ("polls/") and sends the remaining text – "34/"– to the ‘polls.urls’ URLconf for further processing.
->urlpatternsで「policy/」セクションを検索し、後のテキスト「34/」を送信します.
There it matches '/', resulting in a call to the detail() view like so:
->この「34/」はurlsです.pyはpath(「/」,views.detail)セクションであるため、ビューを呼び出します.
すなわち
民調/1コール
ポーリング/1/resultsコール
投票/1/投票コール
ということで
viewを作動させるために、
上記の要求内容を含むHttpResponseオブジェクトを返すか、異常(例えばHttp 404)が発生する必要があります.
HttpResponse
HttpRequestオブジェクトを受信してHttpResponseオブジェクトを返しますが、DjangoはHttpResponseを提供していません.私たちは自分でHttpResponseを作成する必要があります.
まずpolicydirにtemplateddirを作成し、indexを作成します.htmlファイルを作成した後
get templateがどのような方法であるかを決定するには、altキーを使用して確認し、get template(template name)を使用してtemplateをロードします.
templateをロードしてcontextを渡します.
でもこうやってcontextでHttpResponseの部分に対してよく書く部分
これらのよく使われるコードを簡単にしたいです.だからdjangoにもrenderモジュールがあります.
(常にdjangoページでモジュールと関数パラメータを直接チェック)
このように描くと.
HttpResponseはある程度完了しましたが、異常が発生した場合のコードを記述する必要があります.
まずtemplates>policy>detailです.html作成後
views.pyに戻る
get objector 404 obhectが存在しない場合、Http 404が生成される.
この関数はget関数であるdict値であり、listを返すとget list or 404もある.
そうだそうです.
しかし実際にDjangoプロジェクトを始めると世論調査のようなアプリがたくさんありますが、それらの間のURL名はどう違うのでしょうか?urlconfに名前空間を追加するそうです.
urls.pyで
url「policy:detail」に変更します.
request.POSTは、ユーザが鍵によって送信されたデータにアクセスできるようにする辞書などのオブジェクト、要求である.POST「選択」は、選択した調査のIDを文字列として返す.request.POSTの値は常に文字列です.
DjangoはGET資料へのアクセスを同様に要求する.GET–ただし、POSTのみでデータの変更を要求するため、要求コードを明確にしてください.POSTを使用しています.
POST資料が選択されていない場合は、request.POST[選択]にKeyErrorが表示されます.上のコードではKeyErrorがチェックされ、選択されていない場合はエラーメッセージと調査テーブルが返されます.
アンケート数が増加すると、コードは、従来のHttpResponseRedirectではなく、HttpResponseRedirectを返します.HttpResponseRedirectは、ユーザーが再送信するURLである引数を受信します.(この場合、URLの設定方法を見てください:)
As the Python comment above points out, you should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn’t specific to Django; it’s good Web development practice in general. -> POSTデータ処理後はHttpResponseRedirectに戻ります!
この例では、HttpResponseRedirectジェネレータでreverse()関数を使用します.この関数は、ビュー関数でURLをハードコーディングしないようにします.コントロールを渡すビューの名前とURLモードの変数部分を組み合わせてそのビューを指します.ここでは、チュートリアル3章で設定したURLConfを使用します.この逆()呼び出しは、次の文字列を返します.
0. Database setup
最初の設定pyをチェックして

データベースのデフォルトはdefault sqliteです.
他のデータベースを使用する場合
ENGINE : 'django.db.backends.sqlite3', 'django.db.backends.postgresql',
'django.db.backends.mysql',
'django.db.backends.oracle'
上の値で修正して適用します.
TIME_ZONE

この図のTIME ZONE='UTC.
私たちは韓国人なので、asia/soulに変えましょう.
INSTALLED_APPS

前のtutorial 1では、インストールされているappsを見てみましょう.
デフォルトでは、INSTALLED APPSには、Djangoに付属する次のアプリケーションが含まれています.
django.contrib.admin–管理用のサイト.すぐに使えます.
django.contrib.auth–認証システム.
django.contrib.contenttypes–コンテンツタイプのフレームワークに使用します.
django.contrib.セッション–セッションフレームワーク.
django.contrib.Messages–メッセージングフレームワーク.
django.contrib.staticfiles–静的ファイルのフレームワークを管理します.
1.VIEWSの作成
views?
-django特定の機能とテンプレートを提供するWebページ
たとえば、エンベロープは次のとおりです.
同じビューページを持つことができます.

新規ビューコンテンツの作成

viewsを作成した場合はurls

正式文書の説明
When somebody requests a page from your website – say, “/polls/34/”, Django will load the mysite.urls Python module because it’s pointed to by the ROOT_URLCONF setting.
->ブラウザが「/policy/34/」を要求した場合
It finds the variable named urlpatterns and traverses the patterns in order. After finding the match at 'polls/', it strips off the matching text ("polls/") and sends the remaining text – "34/"– to the ‘polls.urls’ URLconf for further processing.
->urlpatternsで「policy/」セクションを検索し、後のテキスト「34/」を送信します.
There it matches '
->この「34/」はurlsです.pyはpath(「
すなわち
民調/1コール

ポーリング/1/resultsコール

投票/1/投票コール

ということで
viewを作動させるために、
上記の要求内容を含むHttpResponseオブジェクトを返すか、異常(例えばHttp 404)が発生する必要があります.
HttpResponse
HttpRequestオブジェクトを受信してHttpResponseオブジェクトを返しますが、DjangoはHttpResponseを提供していません.私たちは自分でHttpResponseを作成する必要があります.
まずpolicydirにtemplateddirを作成し、indexを作成します.htmlファイルを作成した後

from django.http import HttpResponse
from django.template import loader
from .models import Question
def index(request):
latest_question_list = Question.objects.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))
template> index.htmlに接続するコードの作成get templateがどのような方法であるかを決定するには、altキーを使用して確認し、get template(template name)を使用してtemplateをロードします.

templateをロードしてcontextを渡します.
でもこうやってcontextでHttpResponseの部分に対してよく書く部分
これらのよく使われるコードを簡単にしたいです.だからdjangoにもrenderモジュールがあります.


このように描くと.
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
和弦がこんなにきれいになった.(でも最近レンダーをあまり使わないそうなので我慢)HttpResponseはある程度完了しましたが、異常が発生した場合のコードを記述する必要があります.
まずtemplates>policy>detailです.html作成後
{{question}}
入力します.views.pyに戻る
from django.shortcuts import get_object_or_404, render
from .models import Question
# ...
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
に記入get objector 404 obhectが存在しない場合、Http 404が生成される.


そうだそうです.
しかし実際にDjangoプロジェクトを始めると世論調査のようなアプリがたくさんありますが、それらの間のURL名はどう違うのでしょうか?urlconfに名前空間を追加するそうです.
urls.pyで
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
app name=「世論調査」を追加
url「policy:detail」に変更します.
2.投票フォームの設定
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from .models import Choice, Question
# ...
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
上記のコードには、このチュートリアルでは説明されていないものが含まれています.request.POSTは、ユーザが鍵によって送信されたデータにアクセスできるようにする辞書などのオブジェクト、要求である.POST「選択」は、選択した調査のIDを文字列として返す.request.POSTの値は常に文字列です.
DjangoはGET資料へのアクセスを同様に要求する.GET–ただし、POSTのみでデータの変更を要求するため、要求コードを明確にしてください.POSTを使用しています.
POST資料が選択されていない場合は、request.POST[選択]にKeyErrorが表示されます.上のコードではKeyErrorがチェックされ、選択されていない場合はエラーメッセージと調査テーブルが返されます.
アンケート数が増加すると、コードは、従来のHttpResponseRedirectではなく、HttpResponseRedirectを返します.HttpResponseRedirectは、ユーザーが再送信するURLである引数を受信します.(この場合、URLの設定方法を見てください:)
As the Python comment above points out, you should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn’t specific to Django; it’s good Web development practice in general. -> POSTデータ処理後はHttpResponseRedirectに戻ります!

この例では、HttpResponseRedirectジェネレータでreverse()関数を使用します.この関数は、ビュー関数でURLをハードコーディングしないようにします.コントロールを渡すビューの名前とURLモードの変数部分を組み合わせてそのビューを指します.ここでは、チュートリアル3章で設定したURLConfを使用します.この逆()呼び出しは、次の文字列を返します.
Reference
この問題について(Django tutorial 2.), 我々は、より多くの情報をここで見つけました https://velog.io/@jake93/Django-tutorial-2テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol