0👍
lets say you have a set of user’s name displayed , pass the users details (here pk)into the urls parameter. see this example..
views.py
def userlist(request):
list_user= Profile.objects.all()
return render(request,'all_profiles.html',{'list_user':list_user})
this sends ths list of all users to the template using the context dictionary variable "list_user"
Now inject the recieved list of users in a HTML page :
all_profiles.html
{% for uprofile in list_user %}
<a href="{% url 'profile' uprofile.id %}">{{uprofile.username}}</a>
{% endfor %}
now you have a webpage that shows list of all available users and since it is in an anchor tag () and has href with profile/id it will send the corresponding "id" to the url.
example "/profile/1"…
now use the profile id passed with the url by ,
urls.py
path('profile/<int:pk>/', views.profile, name = 'profile'),
here <int:pk>
stores the id of the user coming from the template,
the id can be fetched in the views.py by,
from django.contrib.auth import get_user_model
def profile(request,pk):
user = get_user_model()
user_profile = user.objects.get(pk = pk)
return render(request,"profile.html", {'user_prof' : user_profile})
Now in the profile.html , you can inject any attribute of the User model.
profile.html
<h1>profile {{user_prof.username}} </h1>
<p>{{user_prof.email}}</p>
1👍
In your url.py:
path('my_profile/<int:pk>/', views.profile,name = 'my_profile'),
views.py:
@login_required
def profile(request,pk):
profile_data = Profile.objects.get(pk = pk)
return render(request,'myprofile.html',{"profile_data":profile_data})
def allProfile(request):
all_profile = Profile.objects.all()
return render(request,'all_profile.html',{'all_profile':all_profile})
all_profile.html:
{% for pf in all_profile %}
......
<a class="btn btn-sm" href="{% url 'my_profile' pf.id %}">{{pf.user_name}}</a>
......
{% endfor %}
This is link :
When user will click on this link he goes into their profile.
<a class="btn btn-sm" href="{% url 'my_profile' request.user.profile.id %}">My Profile </a>
- [Answered ]-How do check an instance belongs to the login user?
- [Answered ]-Programmatically identify django many-to-many links
- [Answered ]-'RelatedManager' object has no attribute 'payment_status'
- [Answered ]-Validate multiple foreign keys while saving a ModelForm
0👍
Since you have the Profile model linked up to the inbuilt User model by a foreign key, you can just build a HTML page and inject the current users into the template tags.
For visiting any other users profile,
pass the username of the profile you want to see as url parameter. Receive the parameter in the views.py,
views.py
> def profile (request, username):
> user = User.objects.get(username=username)
now send the user through the Context dictionary and render the required details in your template.
- [Answered ]-Django Channels: How to flush send buffer
- [Answered ]-How to add a second annotation to an already annotated queryset with django models