我已经以admin或任何帐户的身份登录我的网站。我的网站主页上有许多用户名,但是当我单击其中一个以查看他的个人资料时,它总是为我提供admin或任何人的个人资料当前登录的人。
chat.html
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-body">
{% if user.is_authenticated %}
{% if messages %}
<ul>
{% for message in messages %}
<li><a href="{% url 'profile' %}">
{{ message.author }}</a>:{{ message.content }}
</li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
</div>
</div>
</div>
此主页模板
models.py
from django.db import models
from django.contrib.auth.models import User
from PIL import Image
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}-{self.types}'
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
super().save()
img=Image.open(self.image.path)
if img.height>175 or img.width>175:
output_size=(175,175)
img.thumbnail(output_size)
img.save(self.image.path)
views.py
@login_required
def profile(requset):
if requset.method == 'POST':
u_form=UserUpdateForm(requset.POST,instance=requset.user)
p_form=ProfileUpdateForm(requset.POST,requset.FILES,instance=requset.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(requset,f'Your account has been updated')
return redirect('profile')
else:
u_form = UserUpdateForm(instance=requset.user)
p_form = ProfileUpdateForm(instance=requset.user.profile)
context={'u_form':u_form,'p_form':p_form}
return render(requset,'users/profile.html',context)
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('register/',user_views.register,name='register' ),
path('login/',auth_views.LoginView.as_view(template_name='users/login.html'),name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
path('password-reset', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'), name='password_reset'),
path('password-reset/done/', auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'), name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'), name='password_reset_confirm'),
path('password-reset-complete', auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'), name='password_reset_complete'),
path('profile/', user_views.profile, name='profile'),
path('',include('chat.urls'))]
这是任何用户的个人资料模型。如何在我的网页上查看任何用户的任何个人资料?
答案 0 :(得分:0)
chat.html:
{% for message in messages %}
<li><a href="{% url 'profile' pk=message.author.id %}">
{{ message.author }}</a>:{{ message.content }}
</li>
{% endfor %}
urls.py:
path('profile/<int:pk>/', user_views.profile, name='profile'),
views.py
@login_required
def profile(requset, pk):
user = User.objects.get(pk=pk)
if requset.method == 'POST':
u_form=UserUpdateForm(requset.POST,instance=user)
p_form=ProfileUpdateForm(requset.POST,requset.FILES,instance=user.profile)
elif u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(requset,f'Your account has been updated')
return redirect('profile')
else:
u_form = UserUpdateForm(instance=user)
p_form = ProfileUpdateForm(instance=user.profile)
context={'u_form':u_form,'p_form':p_form}
return render(requset,'users/profile.html',context)
记住request.user
是当前登录的用户。