1

I am working on a website using Django. I'm having a difficult time playing around codes, how do I get the ID of a user without passing a primary key (id, pk) in the URL. My code below does not give me a solution to getting the ID of a particular user. When I print (user) it prints the current user logged in. Is there a way to get the ID of other users?

def home(request):
    p=Profile.object.filter(user=request.user)
    u=p.user
    send_request=FriendRequest.objects.filter(from_user=p.user)

def send_request(request, id):
    user=get_object_or_404(User, id=id) 
    frequest=FriendRequest.get_or_creat(from_user=request.user, to_user=user).first()

    path('home', home_view, name='home')

<a href='{% url 'site:send_request'  profile.user.id %}'>Add friend<a>
GistDom Blog
  • 51
  • 1
  • 8
  • what other users? all the users? randomly other users? another user base on what? some filter? – kederrac Mar 07 '20 at 12:13
  • @kederrac.. I have list of my users in homepage. When I click on add friend, it prints the ID of the current user(self). How can I print out the ID of the user I'm sending a request to without a primary key in url? Is this possible in Django? – GistDom Blog Mar 07 '20 at 12:15
  • so you want to get all the users? you have to be more specific, seems like you have a more complex use case, in which case you should post all the details – kederrac Mar 07 '20 at 12:18
  • @kederrac..i just updated my question. Please check – GistDom Blog Mar 07 '20 at 12:30

1 Answers1

1

Use a form with POST method and send the user_id as a parameter but hidden to the user.

<form method="post" action="{% url 'your_url' %}">
    {% csrf_token %}
    <input type="hidden" name="user_id" value="{{ user_id }}" />
    <button type="submit">Add Friend</button>
</form>

You can access the parameter in the view like this

request.POST.get('user_id')
Lag11
  • 542
  • 4
  • 9
  • @Lag11...i Just updated my question. Please check. I was able to get the code working on each users profile when I send a request, because I passed in a primary key to profile view url(id). But in homepage I do not want to pass in a primary key to url (id). It is possible to get the ID of a user without a primary key passed in url? – GistDom Blog Mar 07 '20 at 12:32
  • So you don't want the primary key passed in the url right? But is it okay to be passed into the html as a hidden field? – Lag11 Mar 07 '20 at 12:37
  • @Lag11...how can I pass in the primary key as a hidden field? I am not using a form as a submit button, I have already created the send request view which I updated in my question. – GistDom Blog Mar 07 '20 at 12:39
  • Is there any specific reason not to use a form here?since a button can be designed to look like a link. Reference https://stackoverflow.com/questions/1367409/how-to-make-button-look-like-a-link – Lag11 Mar 07 '20 at 12:41
  • If you still don't want to try with form and if your concern is only about the the primary key being exposed, you can hash your primary keys, use that in the urls and do the encoding&decoding in the backend – Lag11 Mar 07 '20 at 12:48