Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

ajax - Django: How to render real time data in template from another app

I would like to ask for help for my following issue:

Django project with two apps looks like this:

Project

  • CustomerApp
    • templates
      • Customer Html
  • StaffApp
    • templates
      • Staff html

Now..from Customer page I am sending data to two different views (Customer and Staff) like this:

$.ajax({
    dataType: 'json',
    type: 'POST',
    url: "/staff/page1",
    data: JSON.stringify(Items),
    error: function () {
        alert("Error: unable to post data to staff.");
    }
});

$.ajax({
    dataType: 'json',
    type: 'POST',
    url: "customer",
    data: JSON.stringify(Items),
    success: function (response) {
       orderItems = {};
       $("#idCurrent_get").children().remove();
       $("#idCurrent_get").append('<div id="submitText"><br><p>submitted;
    },
    error: function () {
        alert("Reciept: error");
    }
});      

The Staff view looks like this:

def staff(request, *args, **kwargs):

    currentItemsData = dict()

    if request.is_ajax():
        currentItemsData = json.loads(request.body.decode('UTF-8'))
        tID = currentItemsData.get('tNum')
        for key, value in currentItemsData.items():
            if key != 'tNum':                
                newItem = order(Item = key, count = value, tNumber = tID)
                newItem.save()
        return JsonResponse({'success':False, 'errorMsg':"error"})
..
<some logic>
..
return render(request, "staff.html", context)

Now.. When loading Staff html everything seems fine, however when a Customer will hit the button with new data again, obviously the Staff could not render it until I hit refresh

Question is how I can render real time data on Staff page every time Customer will press the button?

I searched that one of the solution could be using Channels? In that case is it possible to send data from Customer to Staff consumer ? Or it is not a correct design ?

Thank you in advance.

question from:https://stackoverflow.com/questions/65862501/django-how-to-render-real-time-data-in-template-from-another-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...