1、配置URLConf
MyDjango > news > urls.py
path('', views.index),
path('details/<int:question:id>', views.details)
2、自定义视图
MyDjango > news > views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
question_list = [
{'id': 1, 'question_text': 'This is question 1'},
{'id': 2, 'question_text': 'This is question 2'},
{'id': 3, 'question_text': 'This is question 3'},
]
context = {'question_list': question_list}
template_name = 'news/details.html'
return render(request, template_name, context)
def details(request, question_id):
return HttpResponse('Hello %s' %question_id)
3、自定义模型
MyDjango > news > templates > news > details.html
{% if question_list %}
<ul>
{% for question in question_list %}
<li><a href="{% url 'news:details' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
4、浏览器请求
http://127.0.0.1:8080/news/