0

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.

dev.ink
  • 380
  • 5
  • 21

2 Answers2

1

I just had a quick overview of your snippet sorry if this not help you but you need to review it a little bit.

You call DoesNotExist on an instance of a Locked model if opp_locked.DoesNotExist: [...] that's not how you should use this exception.

You have a method .exists() that is available but only for Querysets.

Also if your instance does not exists you are alredy returning an Http404 response when you use get_object_or_404() method.

And perhaps you should avoid sharing primary keys between instances and replace them with models.OneToOneField (OneToOnefield)

Malekai
  • 4,765
  • 5
  • 25
  • 60
Chakib
  • 91
  • 1
  • 6
  • Hmm .. thanks, but, that's not solving my question :( Do you have better idea of how should i write this? – dev.ink Dec 20 '19 at 16:43
0

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

dev.ink
  • 380
  • 5
  • 21