Monday, October 5, 2020

Re: LOCK AND UNLOCK PAGES TO SPECIFIC USERS

On Tue, Sep 22, 2020 at 06:55:27PM -0700, Lightning Bit wrote:
> How can one lock a page "unless" the user clicks on a button on a previous
> page? So, for instance, the user will not be able to type in that url and
> get to the page unless they have clicked the button on the previous page at
> some point in time.

When I first started web dev I had many of the same questions. The key
for me was understanding the fundamental underpinnings of the web.
The protocol HTTP is stateless meaning that each request(GET, POST, etc)
has no "knowledge" of previous requests. So if you want a particular
request(GET /my-locked-page) to "remember" that the user clicked on the
button page(POST /my-button-page), you'll need to add something to the
GET /my-locked-page request that will allow Django to know that this
user had previous clicked the button.

The thing that you add to the request is a cookie(invented by Netscape
in the early days to support shopping carts). Django can manage all
this cookie stuff for you via Django Sessions[1]. You could set a
session variable in the button click view:

request.session['has_clicked_button'] = True

Then you could check that in the my-locked-page view:

request.session.get('has_clicked_button', False)


Note: since sessions can be transient, it might makes sense to store
"has_clicked_button" in a Django model instead of a session variable.

> Also, how can you lock someone out of a page unless they have a passcode?
> So, for instance, you type in an input and it has to be a specific word.
> Once that word is typed in then you can unlimited access to the page.

You could use the same concept here as above. Just replace the "button
click" with "correct passcode entered".

[1] https://docs.djangoproject.com/en/3.1/topics/http/sessions/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20201005173635.GO12495%40fattuba.com.

No comments:

Post a Comment