Resolving Template Syntax Error

Due to character limits that discord has I had to resort to hastebin. It has the error message. I am trying to get my ingredients to be updated one ingredient at a time. So that there is a link next to each of the ingredients. I am getting a template syntax error. I hear that django auto increments primary key values. I am looking for the default primary key for an object coming out of a listview. is there a way to find this so that I can name it in my template?

Link to code and error in hastebin due to discord limits Hastebin

Assuming there were no alterations in the process of copying that code to hastebin, the problem is that you have “smart quotes” around the url name and not apostrophes.

1 Like

I have a guess why this error occurs.
If you look at the 0001_initial.py file in the migrations folder, you will see that the primary key of the Ingredients model was created under the name “id”.

 operations = [
        migrations.CreateModel(
            name='Ingredient',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=20)),
                ('quantity', models.FloatField()),
                ('unit', models.CharField()),
                ('price_per_unit', models.FloatField()),
            ],
        ),

This is why in your template you should reference PK of Ingredient model instance as “id”:

 <a target=“_blank” href=“{% url "ingredientupdate" item.id %}”>{{ item.name }}</a>

That is not an issue. Django uses the special name ‘pk’ as a synonym for the primary key of a model regardless of the field being used for the primary key.

See the docs for the pk lookup shortcut.

To be honest, I didn’t know there was a primary key synonym. So thanks for sharing this link.

I admit that my guess was wrong. But I’m glad that I learned something new from it.