Reputation: 801
What I have been trying is to click the post's author(the user that created the post) I want it to redirect me to that user's profile, for example in Instagram when you click the user that is on top of the post-it redirects you to their profile. Every time I do that instead of seeing the post's author profile I see the logged in user profile. I think there is something wrong in the views.py file or in the base.html.
views.py
def profile(request, pk=None):
if pk:
user = get_object_or_404(User, pk=pk)
else:
user = request.user
args = {'user': user}
return render(request, 'profile.html', args)
def home(request):
created_posts = Create.objects.all().order_by("-added_date")
return render(request, 'base.html', {"created_posts": created_posts})
def create(request):
if request.method == 'POST':
created_date = timezone.now()
header1 = request.POST['header']
content1 = request.POST['content']
user = request.user
created_obj = Create.objects.create(added_date=created_date, title=header1, content=content1, user=user)
created_obj.save()
print('create created')
return redirect('home')
else:
print('create not created')
return render(request, 'create.html')
models.py
class Create(models.Model):
added_date = models.DateTimeField()
title = models.CharField(max_length=200)
content = models.CharField(max_length=200)
user = models.ForeignKey(User, related_name='user', on_delete=models.CASCADE, default=1)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
def __str__(self):
return f'{self.user.username} Profile'
urls.py
urlpatterns = [
path('', views.home, name='home'),
path('profile', views.profile, name='profile'),
path('profile/<int:pk>/', views.profile, name='profile_pk'),
path('create', views.create, name='create'),
]
profile.html (shows user's profile)
{% extends 'home.html' %}
{% block body%}
<div class="content-section">
<div class="media">
<img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
<div class="media-body">
<h2 class="account-heading">{{ user.username }}</h2>
<p class="text-secondary">{{ user.email }}</p>
</div>
</div>
</div>
{% endblock %}
base.html (shows all user's post with the user's username)
{% extends 'home.html' %}
{% block body %}
<ul action="{% url 'create' %}" class="container-sm list-group" style="margin-top: 200px;">
{% for created_post in created_posts %}
<li class="list-group-item">{{ created_post.title }}
<a href="{% url 'profile_pk' pk=user.pk %}">{{ created_post.user }}</a>
<p>{{ created_post.content }}</p>
<div class="float-right">
<form action="delete_create/{{ created_post.id }}/" action="post">
<button type="submit" class="btn btn-outline-danger btn-sm">Delete</button>
</form>
</div>
<div class="float-right">
<a href="{% url 'edit' created_post.id %}" class="btn btn-outline-warning btn-sm" style="margin-right: 5px;" role="button">Edit</a>
</div>
</li>
{% endfor %}
</ul>
{% endblock %}
home.html (navbar that shows the loged in user)
<body>
<nav class="navbar navbar-expand-md fixed-top navbar-dark" style="background-color: rgba(0, 0, 0, 0.712);">
<div class="container">
<a class="navbar-brand" href="/">
<img src="static/style/images/logowebdev-png.png" alt="logo" style="width: 60px; height: auto;">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample07" aria-controls="navbarsExample07" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExample07">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item">
<a class="nav-link" style="margin-left: 30px;" href="/">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" style="margin-left: 30px;" href="/profile">profile</a>
</li>
<li class="nav-item">
<a class="nav-link" style="margin-left: 30px;" href="#">Pricing</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
{% if user.is_authenticated %}
<div class="float-right">
<li class="nav-item active">
<a class="nav-link" href="#">New post</a>
</li>
</div>
<div class="float-right">
<li class="nav-item active">
<a class="nav-link" href="">{{ user.username }}</a>
</li>
</div>
<div class="float-right">
<li class="nav-item">
<a class="nav-link" href="/logout">Logout</a>
</li>
</div>
{% else %}
<div class="float-right">
<li class="nav-item">
<a class="nav-link" href="/login">Login</a>
</li>
</div>
<div class="float-right">
<li class="nav-item">
<a class="nav-link" href="/register">Register</a>
</li>
</div>
{% endif %}
</ul>
</div>
</div>
</nav>
{% block body %}
{% endblock %}
</body>
Upvotes: 2
Views: 1099
Reputation: 2627
In your base.html
file change
<a href="{% url 'profile_pk' pk=user.pk %}">{{ created_post.user }}</a>
with
<a href="{% url 'profile_pk' pk=created_post.user.pk %}">{{ created_post.user }}</a>
Because, you must pass id of post owner to your view. When you use only user, django detect authenticated user object.
Upvotes: 2