Tutorial building index template

I just finished this section on creating a template for index.html and displaying it in the “polls” app. When I point at polls/ I got two dotted items (I have two questions in the database) but the content said, "{{ question.question text}}. I believe that is a response from the templating language but I can’t find it. Should I include the code or can you point where to look?

Looks like you’re missing an underscore.and a space, but that should cause a 404 error rather than anything else, so maybe it’s correct in your code…

Whatever the issue is, will probably be in polls/templates/polls/index.html

You should have a line looking like this:
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
(Edit: Just realised it won’t look exactly like that yet, as you’re not that far ahead in the tutorial.
Yours will look more like:
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a>

Note that if you’ve added the __str__ method in the models.py file, you don’t actually need {{ question.question_text }}, you can just use {{ question }} by itself. :slight_smile:

Interesting response. I am in part III of the tutorial. Mine looks like this:
{% if latest_question_list %}


    {% for question in latest_question_list %}
  • <a href="/polls/{{question.id}}/"}}>{{
    question.question_text
    }}

  • {% endfor %}

{% else %}

No polls are available

all in the body of index.html.

OH, and the {% endif %} is after “No polls are available”

It’s difficult to read code in these pages that aren’t quoted. Please enclose your template between lines consisting only of three backtick (`) characters.
Example

# The previous line consists only of ```
{% for question in question_list %}
  {{ question }}
{% endfor %}
# The next line consists only of ```

This maintains your formatting and doesn’t interpret any special characters. (Make sure you use the backtick and not the apostrophe (’)).

OK. thank you for the advise,

‘’’
Hope this works. Putting things in the three ticks seems to cause the "less than 20 characters warning.

Oops, “advice.” Here is the original code enclose in the three tick format.
‘’’
{% if latest_question_list %}

{% for question in latest_question_list %}

  • <a href="/polls/{{question.id}}/"}}>{{
    question.question_text
    }}

{% endfor %}

{% else %}

No polls are available

{% endif %}
‘’’

You need to use backticks (`), not apostrophes (’)

Just responding because I have been busy with the hospitable the last 4 days. Back now. I have never been aware of upticks and I have been working on Macs for a long time. Wow. Thanks. I will try again.

Does that work?
I get this notice that the message has to have at least 20 characters and something about the heart button. Does it ignore text in between the upticks? Where is the heart button?

It looks like you’re just quoting the previous message, and that you’ve enclosed each line between lines of backticks.

Make sure you’ve got:

One line - three backticks.
all your code, single spaced
one line - three backticks

{% if latest_question_list %}
{% for question in latest_question_list %}* <a href="/polls/{{question.id}}/"}}>{{
question.question_text
}}
{% endfor %}
{% else %}
No polls are available
{% endif %}

of course it is not indented properly. I am trying to do this right.

Bingo! Now we can move forward with finding an answer to the original issue.

You’ve got a set of double-braces after the href parameter:
<a href="/polls/{{question.id}}/"}}>
Those last two braces shouldn’t be there.

(If that fixes it, ignore the rest of this message. If that doesn’t fix it, read on…)

Can you share the view that is rendering this template? (Same formatting - enclose the view between lines consisting only of three backticks.)

Also, just to confirm - the issue is that the template is actually rendering the text {{ question.question_text }} as the text within the page?

That was a problem with my rewriting the code for this message. That mistake is not in my code in the “view” file. I am copying the code off my laptop and trying to get the markdown language correct. I will try again here to present the code.

<body
{%  if latest_question_list %}
    <ul>
        {% for question in latest_question_list %}
            <li><a href="/polls/{{ question.id }}/" {{
                        question.question_text
                        }} </a></li>
         {% endfor %}
    </ul>
{% else %}
    <p> No polls are available </>
 {% endif %}
</body>

I have a reason for working off two computers but it may not turn out to be a good one. I am pretty sure that I have copied it correctly. I like this feature of displaying what I have type on the right of the page.

Thank you for your patience. I definitely need it.

Well, there still are a couple things that might be artifacts of your copying this code over:

  • You’re missing the > after the open body tag
  • Your {{ question.question_text }} is split across multiple lines
  • Your </p> tag is missing the p on the third line from the end.

But I’m guessing the key issue here is the second one above.

Ken

Hi Ken,
Thanks. Items 1 and 3 are mistypes in translation. Item 2 is the problem and is now solved. Thank you again. I had split that line on purpose because of the formatting in PyCharm. I thought it was alright. Clearly, I was wrong. Thank you for hanging with me. That meant a lot.

so, now that works. Then I get to the raising the 404 with this code

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})

and I get “unbound local error” for the "question = Question.objects.get(pk=question.id)
line. I believe I followed. the instructions correctly.

Are you sure you’re looking at the right section / question / method?

The reason I’m asking is that in your code fragment you have:

question = Question.objects.get(pk=question_id)
                                           ^

but, in the error message you’re quoting, it has:

question = Question.objects.get(pk=question.id)
                                           ^

Notice in the first example you have question _ id, but in the second, you’re showing question . id

Something’s not matching up here.

(Also, in the future, it might be helpful to post the entire traceback text - there are times when the actual error isn’t what the apparently obvious error is.)

Ken

Thank you. I stared at that and stared at that and didn’t see the difference. The most important thing I am going to learn from all this is how to be very careful. I thought that I was, but now I am pretty sure I am not. Deep appreciation, Ken.