问题
How To Show User Profile To Everyone By Link! in Django
I Want To Show User Profile To Everyone for-example if someone type this in browser domain.com/profile/1
Then Our First User Profile Want To Show
But it's showing blank
It's showing when user login but we need to show to everyone
Here is my detail.html
{% extends 'base.html' %}
{% block body_block %}
<h1 class="posttitle">{{user.username}}</h1>
{% endblock %}
Here is my Views.py
def profile_detail(request,pk):
model = get_object_or_404(User, pk=pk)
return render(request,'profile_detail_view.html')
If You Need More Files Like Model,Views,Url Something Let me down in comment i will update my question
Any Help Will Be Appreciated
Thanks!!
回答1:
You are not passing the user instance to the detail.html .
def profile_detail(request,pk):
user = get_object_or_404(User, pk=pk)
return render(request,'profile_detail_view.html',{'user':user})
回答2:
I am not sure if it is typo or intentional but in your question you have mentioned template name as detail.html
however your view is rendering a different template profile_detail_view.html
.
Also I see you have not passed any context data to the template, so you need to fix that and finally please ensure your urls.py
is setup properly to serve this route.
def profile_detail(request,pk):
user = get_object_or_404(User, pk=pk)
return render(request,'detail.html', {'user':user})
来源:https://stackoverflow.com/questions/58812153/how-to-show-user-profile-to-everyone-by-link-in-django