Python 在Django中返回其他上下文变量
我有一个页面,用户可以在上面搜索其他用户,一旦他们搜索,就会显示一个符合他们搜索条件的用户列表。在搜索结果中的每个用户旁边,我都有一个指向“添加为好友”。每个链接都链接到URL.py文件中的一个python函数,该函数将向数据库中添加请求等。但是,我目前还没有使用AJAX,因为我正在尝试使用或不使用JavaScript都可以。但是一旦调用python函数,我想将一个上下文变量返回到调用该函数的模板,并添加一个变量,以便在模板中检查它,并删除用户单击的链接,但将所有其他链接保留在所有其他用户旁边。python函数如下所示:Python 在Django中返回其他上下文变量,python,django,Python,Django,我有一个页面,用户可以在上面搜索其他用户,一旦他们搜索,就会显示一个符合他们搜索条件的用户列表。在搜索结果中的每个用户旁边,我都有一个指向“添加为好友”。每个链接都链接到URL.py文件中的一个python函数,该函数将向数据库中添加请求等。但是,我目前还没有使用AJAX,因为我正在尝试使用或不使用JavaScript都可以。但是一旦调用python函数,我想将一个上下文变量返回到调用该函数的模板,并添加一个变量,以便在模板中检查它,并删除用户单击的链接,但将所有其他链接保留在所有其他用户旁边。
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})
{% 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 %}
def请求朋友(请求,给朋友):
尝试:
from_friend=request.user
to_friend=CustomUser.objects.get(pk=to_friend)
f=友谊(from_friend=from_friend,to_friend=to_friend)
f、 保存()
f1=友谊(从朋友到朋友,到朋友到朋友)
f1.保存()
尝试:
text=“已请求您作为朋友”%(来自\u friend.username,来自\u friend.username)
n=通知(从用户=从朋友,到用户=到朋友,通知文本=文本)
n、 保存()
响应='请求的朋友'
除:
响应='无法保存通知'
除:
响应='未保存到数据库'
返回TemplateResponse(请求,'users/friend_search.html',{'friend_added':response})
显示用户列表的模板代码如下:
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})
{% 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 %}
{%u在用户%}
{{u.get_full_name}
{%if-not-friend\u added%}{%else%}{{friend\u added}{%endif%}
{%endfor%}
我怎样才能做到这一点?谢谢我没有完全理解代码中缺少了什么变量,只是想在上下文中添加变量
你有,这很方便。可以在上下文词典中手动添加所需内容,也可以在整个站点上需要变量时使用。下面的代码可以完成这项工作。相应地调整模板
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
})
def请求朋友(请求,给朋友):
结果=错误
尝试:
from_friend=request.user
to_friend=CustomUser.objects.get(pk=to_friend)
f=友谊(from_friend=from_friend,to_friend=to_friend)
f、 保存()
f1=友谊(从朋友到朋友,到朋友到朋友)
f1.保存()
尝试:
text=“已请求您作为朋友”%(来自\u friend.username,来自\u friend.username)
n=通知(从用户=从朋友,到用户=到朋友,通知文本=文本)
n、 保存()
响应='请求的朋友'
结果=真
除:
响应='无法保存通知'
除:
响应='未保存到数据库'
返回TemplateResponse(请求,
“users/friend_search.html”,
{
“friend_added”:结果,
“消息”:响应
})