djangodjango-modelsdjango-templatesdjango-viewsdjango-context

Django object "lock" and context rendering


I have a simple(I think) question, about Django context rendering.

I'll step right into it -

Basically what I need is, some temp table, which in my case, I called Locked. And when a user presses a button, which Is a form, that object goes straight to the table Locked(just a simple insert). And inside that table there is a field called is_locked, and if its True, that object needs to go gray, or to have some lock icon inside the html table.

Just some kind of a viewable sign, that an object is inside the table Locked, and that another user can't access it.

But, my problem is, since in my views.py, my lock function is not returning exact html where I want to render that locker icon, instead, it returns another html.

Is there any way, to render same context, on 2 html pages? Thank's.

This is my code :

views.py


def lock(request, pk):
    # Linking by pk.
    opp = get_object_or_404(OpportunityList, pk=pk)
    opp_locked = get_object_or_404(Locked, pk=pk)
    # Taking two parametters for 2 fields.
    eluid = Elementiur.objects.get(eluid=pk)
    user = User.objects.get(username=request.user)

    # Dont bother with this one! Just pulling one field value.
    field_name = 'optika_korisnik'
    obj = OpportunityList.objects.get(pk=pk)
    field_object = OpportunityList._meta.get_field(field_name)
    field_value = getattr(obj, field_object.attname)

    # This is the main part! This is where i'm inserting data into Locked table.
    if opp_locked.DoesNotExist:
        opp_locked.id = int(eluid.eluid)
        opp_locked.locked_eluid = eluid
        opp_locked.locked_comment = field_value
        opp_locked.locked_user = user
        opp_locked.locked_name = 'Zaključao korisnik - ' + request.user.username
        opp_locked.is_locked = True
        opp_locked.save()
        # This is what has to be returned, but i need context on the other page.
        return render(request, 'opportunity/detalji/poziv.html',
                      {'opp': opp, 'locked': opp_locked})
    else:
        # This return has the context that i need(from the first opp_locked variable)
        return render(request, 'opportunity/opp_optika.html', {'locked_test': opp_locked})

I can provide more code, but i think that it's not important for this type of question, because all of the logic is happening inside the lock finction, and last two returns.


Solution

  • Since i got no answers, i added a new field, is_locked, into my Locked model and that solved it.