Tourist Attractions with Templates Project - Part 6, 14, 15

Hi there, I just want to share my experience by doing the “Tourist Attractions With Templates” project of the build python web apps with django course. Im new to python (just started to learn this year, and it is the first approach to django)

PART 6
I was doing part 6 and when I refreshed the page, it output some error. ThenI check the video walkthrough, and I am doing exactly the same. But finally I figured it out by myself, if you put this:

{% extends './base.html' %}

instead of this:

{% extends 'tourist_attractions/base.html' %}

it works just fine

PART 14 & 15

I realized there was a problem by part 15. And then, looking up some other topics in the forum, I found a way to solve the mistery. When you are asked to do this in part 14 (and the guy in the video actually does it and works):

<td><a href="{% url 'details' item.state|slufigy %}">State Details</a></td>

just do this

<td><a href="{% url 'details' statename=item.state|slufigy %}">State Details</a></td>

the statename variable name is free to choose, just make sure you use the same in part 15, when you´re asked to access the statename. If you dont include the slugify filter, it will work for any state that is composed of one word, but it wont work when theres 2 or more words.

And then… if you do this for 15, you will be jut fine. I dont understand how exactly is that a variable declared in home.html turns out to be in the “scope” of details.html, since details.html only extends base.html (if somebody understands why this works please explain to me), but you can try by yourself. I tried accessing the state name in many other ways, but it didnt work :frowning:

<h1>This is a list of toursit attractions for {{statename}}</h1>

here is my final code

{% extends './base.html' %}

{% load static %}

{% block head %}
<link rel="stylesheet" href="{% static '../static/tourist_attractions/style.css' %}">
{% endblock %}

{% block content %}
<h1>This is a list of attractions in America!</h1>
<table>
  <tr>
    <th>Attraction</th>
    <th>State</th>
    <th>State details</th>
    {% for item in attractions|dictsort:"state" %}
    <tr>
      <td>{{item.attraction_name}}</td>
      <td>{{item.state}}</td>
      <td><a href="{% url 'details' item.state|slugify %}">State Details</a></td>
    </tr>
    {% endfor %}
  </tr>
</table>
{% endblock %}
{% extends './base.html' %}

{% load static %}

{% block head %}
<link rel="stylesheet" href="{% static '../static/tourist_attractions/style.css' %}">
{% endblock %}

{% block content %}
<h1>This is a list of toursit attractions for {{statename}}</h1>
<table>
  <tr>
    <th>Attraction</th>
    <th>State</th>
    {% for item in attractions|dictsort:"state" %}
    {% if statename == item.state|lower%}
    <tr>
      <td>{{item.attraction_name}}</td>
      <td>{{item.state}}</td>
    </tr>
    {% endif %}
    {% endfor %}
  </tr>
</table>


{% endblock %}
1 Like