djangoビューview
4949 ワード
djangoのビューには2種類あり、1つは
最初のビューの作成
function
式、1つはclass
式settingファイルの中のROOT_URLCONF
がルーティングマッチングのメインurlファイルを設定しています最初のビューの作成 polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
次に、適用されたurlファイルにurlマッチングを登録します。 polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
プロジェクトのメインurlファイルにurlをアプリケーションに割り当てる 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/', include(admin.site.urls)),
]
正規マッチングurlを使用してパラメータを取得 -- polls/views.py
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
--polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P[0-9]+)/vote/$', views.vote, name='vote'),
]
:
グループ内で変数の名前を定義することなく、パラメータを順番に受信することもでき、名前を定義した場合、名前が対応して受信しなければならない.
djangoテンプレートの低バージョンモードをレンダリングする from django.http import HttpResponse
from django.template import RequestContext, 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 = RequestContext(request, {
'latest_question_list': latest_question_list,
})
return HttpResponse(template.render(context))
テンプレートをrenderでレンダリングするには polls/views.py
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)
-- {{}}
polls/templates/polls/detail.html
{{ question }}
404エラーを引き起こす低レベル方式 polls/views.py
from django.http import Http404
from django.shortcuts import render
from .models import Question
# ...
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'polls/detail.html', {'question': question})
404エラーを引き起こす高度な方法get_object_or_404 polls/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})
テンプレート言語のurl接続指向の処理
保守性が悪すぎます{{ question.question_text }}
はname
を使用する必要があります{{ question.question_text }}
-- app url name name=detail
--url(r'^(?P[0-9]+)/$', views.detail, name='detail')
-- url, question.id url
app url
ネーミングスペースnamespace
を使用する必要があります url namespace
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
app url name
mysite/urls.py
url(r'^specifics/(?P[0-9]+)/$', views.detail, name='detail')
polls/templates/polls/index.html
{{ question.question_text }}
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
プロジェクトのメインurlファイルにurlをアプリケーションに割り当てる 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/', include(admin.site.urls)),
]
正規マッチングurlを使用してパラメータを取得 -- polls/views.py
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
--polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P[0-9]+)/vote/$', views.vote, name='vote'),
]
:
グループ内で変数の名前を定義することなく、パラメータを順番に受信することもでき、名前を定義した場合、名前が対応して受信しなければならない.
djangoテンプレートの低バージョンモードをレンダリングする from django.http import HttpResponse
from django.template import RequestContext, 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 = RequestContext(request, {
'latest_question_list': latest_question_list,
})
return HttpResponse(template.render(context))
テンプレートをrenderでレンダリングするには polls/views.py
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)
-- {{}}
polls/templates/polls/detail.html
{{ question }}
404エラーを引き起こす低レベル方式 polls/views.py
from django.http import Http404
from django.shortcuts import render
from .models import Question
# ...
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'polls/detail.html', {'question': question})
404エラーを引き起こす高度な方法get_object_or_404 polls/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})
テンプレート言語のurl接続指向の処理
保守性が悪すぎます{{ question.question_text }}
はname
を使用する必要があります{{ question.question_text }}
-- app url name name=detail
--url(r'^(?P[0-9]+)/$', views.detail, name='detail')
-- url, question.id url
app url
ネーミングスペースnamespace
を使用する必要があります url namespace
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
app url name
mysite/urls.py
url(r'^specifics/(?P[0-9]+)/$', views.detail, name='detail')
polls/templates/polls/index.html
{{ question.question_text }}
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/', include(admin.site.urls)),
]
-- polls/views.py
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
--polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P[0-9]+)/vote/$', views.vote, name='vote'),
]
:
グループ内で変数の名前を定義することなく、パラメータを順番に受信することもでき、名前を定義した場合、名前が対応して受信しなければならない.djangoテンプレートの低バージョンモードをレンダリングする from django.http import HttpResponse
from django.template import RequestContext, 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 = RequestContext(request, {
'latest_question_list': latest_question_list,
})
return HttpResponse(template.render(context))
テンプレートをrenderでレンダリングするには polls/views.py
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)
-- {{}}
polls/templates/polls/detail.html
{{ question }}
404エラーを引き起こす低レベル方式 polls/views.py
from django.http import Http404
from django.shortcuts import render
from .models import Question
# ...
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'polls/detail.html', {'question': question})
404エラーを引き起こす高度な方法get_object_or_404 polls/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})
テンプレート言語のurl接続指向の処理
保守性が悪すぎます{{ question.question_text }}
はname
を使用する必要があります{{ question.question_text }}
-- app url name name=detail
--url(r'^(?P[0-9]+)/$', views.detail, name='detail')
-- url, question.id url
app url
ネーミングスペースnamespace
を使用する必要があります url namespace
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
app url name
mysite/urls.py
url(r'^specifics/(?P[0-9]+)/$', views.detail, name='detail')
polls/templates/polls/index.html
{{ question.question_text }}
from django.http import HttpResponse
from django.template import RequestContext, 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 = RequestContext(request, {
'latest_question_list': latest_question_list,
})
return HttpResponse(template.render(context))
polls/views.py
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)
-- {{}}
polls/templates/polls/detail.html
{{ question }}
404エラーを引き起こす低レベル方式 polls/views.py
from django.http import Http404
from django.shortcuts import render
from .models import Question
# ...
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'polls/detail.html', {'question': question})
404エラーを引き起こす高度な方法get_object_or_404 polls/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})
テンプレート言語のurl接続指向の処理
保守性が悪すぎます{{ question.question_text }}
はname
を使用する必要があります{{ question.question_text }}
-- app url name name=detail
--url(r'^(?P[0-9]+)/$', views.detail, name='detail')
-- url, question.id url
app url
ネーミングスペースnamespace
を使用する必要があります url namespace
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
app url name
mysite/urls.py
url(r'^specifics/(?P[0-9]+)/$', views.detail, name='detail')
polls/templates/polls/index.html
{{ question.question_text }}
polls/views.py
from django.http import Http404
from django.shortcuts import render
from .models import Question
# ...
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'polls/detail.html', {'question': question})
polls/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})
テンプレート言語のurl接続指向の処理
保守性が悪すぎます{{ question.question_text }}
はname
を使用する必要があります{{ question.question_text }}
-- app url name name=detail
--url(r'^(?P[0-9]+)/$', views.detail, name='detail')
-- url, question.id url
app url
ネーミングスペースnamespace
を使用する必要があります url namespace
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
app url name
mysite/urls.py
url(r'^specifics/(?P[0-9]+)/$', views.detail, name='detail')
polls/templates/polls/index.html
{{ question.question_text }}
{{ question.question_text }}
{{ question.question_text }}
-- app url name name=detail
--url(r'^(?P[0-9]+)/$', views.detail, name='detail')
-- url, question.id url
url namespace
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
app url name
mysite/urls.py
url(r'^specifics/(?P[0-9]+)/$', views.detail, name='detail')
polls/templates/polls/index.html
{{ question.question_text }}