Issue
I'm trying to create a dynamic URL pattern where there's an ID passed into the URL and used the retrieve a piece of information from the database. Here's the definition of the pattern:
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')
]
and here's the piece of code used to retrieve the information:
def detail(request, question_id):
question = get_object_or_404(Questions, pk = question_id)
return render(request, 'polls/detail.html', {'question': question})
the user seems to be redirected to a 404 page no matter what id is passed in the url.
this is my index.html:
<!DOCTYPE html>
<html lang="en">
<!-- head -->
<body>
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{question.id}}/">{{ question.qtext }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</body>
</html>
Solution
You can provide your URL in the HTML document like this:
<a href="{% url 'detail' question_id=1 %}">Get Detail</a>
Answered By - Helge Schneider
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.