私はDjangoチュートリアルを進んでいます。私は投票アプリケーションを作成するステップ3にいます。 "question_id"と呼ばれる変数があり、正確にどこが定義されているのか、どこから来ているのかわかりません。私は以下のファイルを投稿します。私の唯一の推測では、Models.pyにクラスQuestionが定義されているときに、この変数がDjangoによって何らかの形で内部的に作成されますが、わかりません。それは "質問"クラスで定義されていません。ここでDjangoを学んでいますが、変数と混同しています
私のファイルは、以下のとおりです。
views.py
from django.shortcuts import render
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]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
#def index(request):
# return HttpResponse("Hello, world. You're at the polls index.")
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "Your looking at result of question %s."
return HttpResponse(response % question_id)
def vote(reqeust, question_id):
return HttpResponse("You're voting on question %s." % question_id)
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<question_id>[0-9]+/$)', views.detail, name='detail'),
#ex: /polls/5/result/
url(r'^(?P<question_id>[0-9]+/results/$)', views.results, name='results'),
#ex: /polls/5/vote
url(r'^(?P<question_id>[0-9]+/vote/$)', views.vote, name='vote'),
]
models.py
import datetime
from django.db import models
from django.utils import timezone
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
は 、あなたの助けをありがとうネーミン
Djangoチュートリアル(https://docs.djangoproject.com/ja/1.10/intro/tutorial03/)によると、 HTTPリクエストのパラメータURLはurls.pyで渡され、paramsは提供されたURLの一部です。 – jdv