私は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 %}
home.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 %}
ビュー.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 に「誰かがすぐに到着します」というメッセージを表示するにはどうすればよいですか?