Search code examples
pythondjangoautocompletepycharm

PyCharm - Django tutorial autocompleting in HTML files


I'm currently working through the Django tutorial, and I've found something that's a bit troublesome and I'm not sure if there's a workaround. This is an HTML file opened up in PyCharm, and there are two models that I've created: Question and Choice.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ question.question_text }}</title>
</head>
<body>
<h1>{{ question.question_text }}</h1>
<ul>
    {% for choice in question.choice_set.all %}
        <li>{{ choice.choice_text }} -- {{ choice. }}</li>
   {% endfor %}
</ul>
</body>
</html>

When I get to this part, there are no suggestions being shown for choice, even though there is stuff there.

        <li>{{ choice.choice_text }} -- {{ choice. }}</li>

For example, the part that I haven't finished is supposed to be choice.votes, but I have to type .votes manually because it doesn't autocomplete.

But, I'm able to autocomplete the question_text for question. Is there a reason that the autocomplete doesn't work for choice? Is there any way to "fix" it, or is that just the intended behavior?

I've only just started programming in Python but I've worked on several large projects in Java and I know that without Eclipse's autocomplete for Java variables it'd be a huge pain to remember the names of every variable.

Like, for the tutorial the Choice model just has a couple fields, but what if I'm working on something larger that has hundreds of variables? Do I just have to manually remember the variable names when using them in this context?


Solution

  • In general, PyCharm doesn't autocomplete django template syntax because it has no clue which view will be rendering the template. So it has no clue what to expect as far as context goes.

    That being said, I imagine your autocomplete came from your <title> tag above the <h1>{{ question.question_text }}</h1>. You've already accessed question_text from question before, so PyCharm is aware that the question object contains question_text.

    PyCharm's autocomplete in .py files is generally very good, but yes, in templates you'll be accessing attributes without the help of autocomplete.