vt-cwalker
vt-cwalker

Reputation: 805

returning additional context variables in Django

I have a page that users can search for other users on and once they search, a list of users is displayed that match their search criteria. Next to each user in the search results, I have a link to "Add as friend'. Each link is linked to a python function in the urls.py file that will add the request to the database etc. However, I am not using AJAX for this as of yet as I am trying to make everything that I can work with or without JavaScript. But once the python function is called, I want to return a context variable back to the template that called the function and add a variable so that I can check for it in the template and remove the link that the user clicked but leave all other links next to all other users. The python function is below:

def request_friend(request,to_friend):
    try:
        from_friend = request.user
        to_friend = CustomUser.objects.get(pk=to_friend)
        f = Friendship(from_friend=from_friend,to_friend=to_friend)
        f.save()
        f1 = Friendship(from_friend=to_friend,to_friend=from_friend)
        f1.save()
        try:
            text = "<a href='/%s/'>%s</a> has requested you as a friend" % (from_friend.username,from_friend.username)
            n = Notification(from_user=from_friend,to_user=to_friend,notification_text=text)
            n.save()
            response = 'Friend Requested'
        except:
            response = 'Couldnt save notification'
    except:
        response = 'Did not save to database'
    return TemplateResponse(request,'users/friend_search.html',{'friend_added':response})

And the template code that shows the list of users is below:

{% for u in users %}
<div id="results">
    <img src="{{ u.profile_pic }}" class="xsmall-pic" /> <a href="/{{ u.username }}/">{{ u.username }}</a><br />
    <span class="small-date">{{ u.get_full_name }}</span>
    <span class="floatR" id="user_{{ u.id }}_link">{% if not friend_added %}<a href="/users/requests/friends/{{ u.id }}/" id="{{ u.id }}" class="user_link" onclick="return request_friend({{ u.id }});">Add as friend</a>{% else %}{{ friend_added }}{% endif %}</span>

</div>{% endfor %}

How can I accomplish this? Thanks

Upvotes: 0

Views: 412

Answers (2)

Narendra Kamma
Narendra Kamma

Reputation: 1441

following code do the work. Adjust your template accordingly.

def request_friend(request,to_friend):
    result = False
    try:
        from_friend = request.user
        to_friend = CustomUser.objects.get(pk=to_friend)
        f = Friendship(from_friend=from_friend,to_friend=to_friend)
        f.save()
        f1 = Friendship(from_friend=to_friend,to_friend=from_friend)
        f1.save()
        try:
            text = "<a href='/%s/'>%s</a> has requested you as a friend" % (from_friend.username,from_friend.username)
            n = Notification(from_user=from_friend,to_user=to_friend,notification_text=text)
            n.save()
            response = 'Friend Requested'
            result = True
        except:
            response = 'Couldnt save notification'
    except:
        response = 'Did not save to database'
    return TemplateResponse(request,
                            'users/friend_search.html',
                            {
                            'friend_added': result, 
                            'message': response
                            })

Upvotes: 0

Narsilou
Narsilou

Reputation: 321

I didn't fully understand what variable you were missing in your code but to add variables to a context you have render_to_response that is very handy. Either manually add what you need in the context dictionnary or use context_processors if you need the variables on you whole site.

Upvotes: 1

Related Questions