[Answered ]-How to make URL for visiting post author's profile page from the post page

0๐Ÿ‘

โœ…

You can add a method get_user_public_profile_url in your Product model, which returns the profile URL of the product.user

from django.urls import reverse

class Product(models.Model):
    user = models.ForeignKey(User,  on_delete=models.CASCADE)
    name = models.CharField(max_length=50)
    ...

    def get_user_public_profile_url(self):
        return reverse('Public-Profile', kwargs={'slug': self.user.profile.slug})

and in your template, change this line

<a href="{% url 'Public-Profile' %}">

to

<a href="{{ product.get_user_public_profile_url }}">
๐Ÿ‘คSumithran

1๐Ÿ‘

Try adding a slug parameter to your url, that leads to the product related user profile slug product.user.profile.slug

{% for product in products %}
    <div class="col-md-3">
        <div class="card my-2 shadow-sm bg-body">
            <div class="card-head">
                <a href="{% url 'Public-Profile' slug=product.user.profile.slug %}">
                    <span class="material-symbols-outlined">
                        account_circle
                    </span>
                    {{ product.user.username }}
                </a>
            </div>
        </div>
    </div>
{% endfor %}
๐Ÿ‘คweAreStarsDust

Leave a comment