Abby Kabalin
Abby Kabalin

Reputation: 37

Checking that model field is in url Django 4

My group project peeps and I have been stuck on this for a minute. We're making a travel web app and we have a destinations app and an attractions app. We want to check that the attraction.location is in the url so that it only displays that destinations attractions, otherwise it will show all attractions ever created, regardless of destination. We have tried multiple {% if ... in request.get_full_path %} and such and the tags we've tried are attraction.location and attraction.location.pk. Any advice would be so helpful. Here is the model and the attraction_list.html snipped that we are trying to put it in

models.py

class Attraction(models.Model):
    location = models.ForeignKey(
        Destination,
        on_delete=models.CASCADE,
    )
    name = models.CharField(primary_key=True, max_length=255)
    description = models.TextField(blank=False)
    address = models.TextField()
    rating = models.IntegerField(
        blank=False, validators=[MaxValueValidator(5), MinValueValidator(1)]
    )
    tags = models.TextField()
    numberReviews = models.IntegerField(default=1)
    date = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("attraction_detail", kwargs={"pk": self.pk})

attraction_list.html

{% for attraction in attraction_list %}
{# trying to use if in statement to check if attraction.location is in the full path #}

            <div class="card">
                <div class="card-header">
                    <span class="fw-bold">
                        <a href="{{ attraction.get_absolute_url }}">{{ attraction.name }}</a>
                    </span> &middot;
                    <span class="text-muted">by {{ attraction.author }} |
                        {{ attraction.date }}</span>
                </div>
                <div class="card-body">
                    {{ attraction.description }}
                    {% if attraction.author.pk == request.user.pk %}
                    <a href="{% url 'attraction_edit' attraction.pk %}">Edit</a>
                    <a href="{% url 'attraction_delete' attraction.pk %}">Delete</a>
                    {% endif %}
                    <a href="{{ attraction.get_absolute_url }}">New Comment</a>
                </div>
                <div class="card-footer text-center text-muted">
                    {% for attractioncomment in attraction.attractioncomment_set.all %}
                    <p>
                        <span class="fw-bold">
                            {{ attractioncomment.author }}
                        </span>
                        {{ attractioncomment }}
                    </p>
                    {% endfor %}
                </div>
            </div>

Upvotes: 0

Views: 45

Answers (1)

Kiarash Gh
Kiarash Gh

Reputation: 390

you can use the {% if ... in ... %} template tag to check if the attraction.location is in the URL, in dajngo templates you can access the URL using request.get_full_path

assuming that attraction.location is a ForeignKey field and id is the primary key of the Destination model and destination ID is present in the URL, you can use attraction.location.id :

{% for attraction in attraction_list%}
    {% if attraction.location.id in request.get_full_path %}
        <div class="card">
             <!-- your existing card code -->
        </div>
    {% endif %}
{% endfor %}

I hope this helps

Upvotes: 0

Related Questions