在我的Django站点上,我想在用户登录站点时创建一个对象.我在互联网上搜索了它,我决定在context_processors.py中编写一个方法.所以我写了;
def check_online_status(request):
user_status = None
if request.user.is_authenticated():
user_status = UserStatus.objects.create(user_id=request.user.id)
user_status.status_type = "online"
user_status.save()
return {
'user_status': user_status,
}
Run Code Online (Sandbox Code Playgroud)
这是问题所在; 我的check_online_status()方法在每个请求中被触发但我想立即触发我的方法,只有当用户登录时.
你能帮帮我吗?
这是我的HTML文件的一部分;
<script type="text/template" id="_profile">
<div class="modalDialog">
<div class="profile">
<div class="box clearfix">
<div class="box-header clearfix">
<span class="box-title l">{% trans 'PROFILE' %}</span>
<a href="#" style="margin-left: 10px;" id="profile_export" class="l"><span class="icon-download"></span>{% trans 'Profile Export' %}</a>
<div class="close-profile icon-cancel-circled r"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
和
$('.profile').on('click', '#profile_export', function (e) {
e.preventDefault();
$.get(root + '/profile/export/', function(resp) {
// DO something
Run Code Online (Sandbox Code Playgroud)
我已经看了一些"jQuery中的事件委托"的例子,所以我写了上面的函数.可能是错的,因为当我点击它时,它什么也没做.它只是放在#当前URL的末尾.
谁能告诉我这里有什么问题?为什么当我点击#profile_export它不会触发"profile/export /"的请求?
谢谢