Shreyash mishra
Shreyash mishra

Reputation: 790

how i can assign a specific link to every user by which i can visit his profile

i want to assign a link for every user by which anyone can visit his profile i did this to achieve that hting but it doesn't work how i can do that

my urls.py

from django.conf.urls import url
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views

from .views import SearchResultsView

# Template Urls!
app_name = 'accounts'

urlpatterns = [
    path('Skolar/',views.base, name= 'base'),
    path('Register/',views.register,name='register'),
    path('login/', views.my_login_view, name='login'),
    path('logout/',views.user_logout,name='logout'),
    path('<slug:username>/',views.profile, name='profile'),
    path('EditProfile/',views.update_profile,name='editprofile'),
    path('search/', SearchResultsView.as_view(), name='search'),
]

my models.py for every user it created automatically with the help of signal

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.db.models.signals import post_save
from django.dispatch import receiver

# Create your models here.

class Profile(models.Model):
    user = models.OneToOneField(User ,on_delete=models.CASCADE,)
    profile_pic = models.ImageField(upload_to='profile_pics',default='default.png',)
    twitter = models.URLField(max_length=80, blank=True,)
    facebook = models.URLField(max_length=80, blank=True,)
    about = models.CharField(max_length=1500, blank=True,)
    joined_date = models.DateTimeField(default=timezone.now,editable=False)
    dob = models.DateField(blank=True,null=True)
    update_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.user.username

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, *args, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

how i can assign that by which that link ony open that user profile which is assigned to it please help if you know how to do it

Upvotes: 1

Views: 139

Answers (3)

SANGEETH SUBRAMONIAM
SANGEETH SUBRAMONIAM

Reputation: 1086

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>

Upvotes: 0

Pradip Kachhadiya
Pradip Kachhadiya

Reputation: 2235

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>

Upvotes: 1

SANGEETH SUBRAMONIAM
SANGEETH SUBRAMONIAM

Reputation: 1086

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.

Upvotes: 0

Related Questions