我有一个像 uber 一样的 django 应用程序。客户可以表示他们正在寻找提供服务的人,而同一地区希望成为服务提供商的人可以查看积极寻找客户的列表。当客户按下表示他们正在寻找服务的按钮时,他们会被重定向到一个页面,上面写着"谢谢,当有人在路上时,您将收到通知",并将属性active
设置为True
。这会将它们显示在前面提到的列表中,可以在其中"认领客户"。当服务提供商声明客户时。我希望客户的页面显示"有人很快就会到场"或类似性质的内容。但是,如何让客户的页面 (customer_active.html) 知道active
已设置为False
(即已声明),然后在发生该事件时显示一条消息?我已经读过关于可能使用 django 信号或 ajax/jquery 的信息,但我不知道正确的路由是什么,也不知道如何在所述路由中实现解决方案。我有以下代码:
models.py:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
active = models.BooleanField(default = False)
urls.py
urlpatterns = [
#home
url(r'^home/$', views.home, name = 'home'),
#/claim/user_id
url(r'^claim/(?P<user_id>[0-9]+)/$', views.ClaimView.as_view(), name = "claim"),
#/active
url(r'^active/$', views.customer_active, name='customer_active'),
]
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
customer_active.html(客户在表示他们正在寻找服务后正在查看此内容):
{% extends 'core/base.html' %}
{% block head %}
{% load static %}
<link rel="stylesheet" href="{% static 'core/customer_active.css' %}">
<title>Active</title>
{% endblock %}
{% block body %}
<div class="container text-center">
<div class="row">
<div class="col-md-12 mb-3">
<h1 class="lead">Thank you, {{ user.first_name}} {{user.last_name }}, you will be alerted
when someone claims your laundry</h1>
</div>
</div>
</div>
{% endblock %}
首页.html:
{% for customer in customers%}
<tr>
<td style = "text-align:center">{{ customer.user.first_name|title }} {{ customer.user.last_name|title }}</td>
<td style = "text-align:center">{{ customer.user.profile.address|title }}</td>
<td style = "text-align:center"><a href="{% url 'claim' user_id=customer.user.id %}">Claim</a></td>
</tr>
{% endfor %}
views.py:
#the service provider that claimed a customer gets redirected to 'claim.html', upon this 'active' gets set to False
class ClaimView(View):
def get(self, request, user_id, *args, **kwargs):
customer = User.objects.get(id=user_id)
customer.profile.active = False
customer.save()
return render(request, 'core/claim.html', {'customer': customer})
def customer_active(request):
request.user.profile.active = True;
request.user.save()
return render(request, 'core/customer_active.html', {'user': request.user})
如何使用 ajax/jquery 在服务提供商声称客户时显示一条消息,指出"很快就会有人在那里"customer_active.html?
因此,如果客户在customer_active.html页面中,当服务提供商声明客户时,应在customer_active.html页面中提醒客户。如果我正确感知了问题,那么只需创建一个计时器,该计时器将每隔几秒钟发送一次Ajax呼叫,以检查服务提供商是否已声明客户。
setInterval(function(){
minAjax({
url:"/serviceprovider/",//request URL
type:"GET",//Request type GET/POST
//CALLBACK FUNCTION with RESPONSE as argument
success: function(data){
//check if someone has claimed
if (data == true){
alert("Someone will be there shortly");
}
}
});
}, 5000); //5000 represents 5 seconds
Views.py
@login_required
def serviceprovider(request):
claimed=False
claim=Model.objects.filter(user=User.objects.get(username=request.user), claimed=True)
if claim:
claimed=True
return HttpResponse(json.dumps(claimed))
urls.py
url(r'^serviceprovider/', views.serviceprovider, name = 'serviceprovider'),
你需要minAjax才能工作,它是一个使用纯javascript的非常小的库。只需下载库并将其放入您的js文件夹中,然后导入即可。我还没有测试上面的例子,但我在一个项目中使用了这种方法并且它有效。这只是为了引导您走向正确的方向。祝你好运!