I have a social media website that will allow a user to view another users profile page by clicking their username from one of their posts that appears in the feed section of my page. I have view_profile view created but need to pass in the desired users username into the view, to only show posts, activity, etc... made by that specific user. How do i pass the username into my view?
view_profile.html
<section id="main_feed_section" class="feed_sections">
{% for post in posts %}
<div class="post_main_container">
<section class="post_title">
<h1> <a href="{% url 'view_profile' %}">{{ post.user_id.username }}</a>{{ post.title }}</h1>
</section>
<section class="post_main_body">
<p>{{ post.caption }}</p>
</section>
<section class="post_footer">
<p>{{ post.date_posted }}</p>
</section>
</div>
{% endfor %}
</section>
views.py
def view_profile(request):
account = "logan9997" #if a post made by logan9997 was clicked
posts = [post for post in Posts.objects.all()
if post.user_id.username == account]
followers = [follower for follower in Relationships.objects.all()
if follower.acc_followed_id.username == account]
context = {
"title":account,
"posts":posts,
"followers":followers
}
return render(request, "myApp/view_profile.html", context)
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("home/", views.home, name="home"),
path("create_post/", views.create_post, name="create_post"),
path('view_profile/<str:username>', views.view_profile, name="view_profile"),
]