Hi .
I hope you are fine .
I am making a django restaurant project and I have few questions that I will thank if anyone give a piece of guidance to me to solve them .
The first and main problem is that I have put a link in the footer of the html template for the admin to login into administration and I want when the link change the web page to the admin , the django default admin login page appear every time even the admin has logged in before .
Can anyone help to solve this problem ?
Thanks .
Regards,
Are you setting the link to the admin site (typically â/admin/â) or is the link pointing to the admin login page?
If theyâre logging out or logging in as different users, they will need to log in again.
Also, the way Chrome works, you canât be logged on to the same site as two different users in different tabs, unless one of them is an âincognitoâ tab.
Hi dear Mr .Whitesell .
Thank you for your response .
My answer is the first one .
How do I have to do to apear the django admin login page for every time that anybody click on that link ?
The admin takes care of that automatically. If youâre not logged on - or not logged on as a user with the is_staff
or is_superuser
attribute set, you will be redirected to the admin login page.
Dear Mr. Whitesell I understand what you are trying to say but I mean :
I see your picture and the link youâre referencing, but I donât understand what your question is - what youâre trying to achieve or what it is that you are trying that isnât working.
And when clicking on that if the admin did not login before it show the page below that is OK .
But the admin has logged in before it goes directly to the administration without any admin login page such as below :
but the thing that I want is to show the admin login page anytime that somebody or admin click on the admin url .
Youâve got a couple of different choices.
You can define your sessions to expire after some period of time or when the browser is closed. This allows someone who has logged in to revisit the admin without logging back in within the defined period of time.
See:
You could also create a custom view that your link points to - this custom view would invalidate the current userâs session and then redirect them to the admin. That would force a re-authentication whenever that link is used. (However, this does not prevent someone from entering the /admin/
url directly and bypassing that view.)
Thanks .
That is good .
But please can you explain the second solution a bit more I mean what do I have to do in views.py ?
See How to log a user out. Your view should log out the current user and then redirect them to the /admin/
page.
Thank you dear Mr.Whitesell for solving this question .
But my last question is that in my third photo that I sent ( administration page ) if a customer do his meal reservation , an object in the reservation model will be created automatically due to my code that is fine , but I want to also put a number of unread reservation objects for the admin next to the change icon in the reservations row .
How do I have to do to do this ?
I am not aware of any facility to do that.
It seems to me like youâre trying to rely upon the admin to do things it was never designed to do.
In such cases, I always refer people to the first three paragraphs in the Django Admin site docs.
Excerpts:
The adminâs recommended use is limited to an organizationâs internal management tool. Itâs not intended for building your entire front end around.
and
If you need to provide a more process-centric interface that abstracts away the implementation details of database tables and fields, then itâs probably time to write your own views.
Hi dear Mr.Whitesell, I hope you are fine .
I am at the end of that restaurant project that I have talked to you before about it here .
I have two questions.
-I have a model and I want something to be happen .I mean I want this, that if the admin wants to create an object of this model , do something ,for example email something ,before saving the object .How can I do this ?
-How to forbid the admin in the Django Users model to change the objects but give the permission to create objects ?
Thanks .
Kind Regards .
If you want something to happen every time an instance of an object is created, whether or not itâs being done in the admin, you can override the modelâs save method to do âsomethingâ before the instance is actually saved. See Saving objects and Overriding predefined model methods for more details. (Since this method is both used for creating an object and updating an existing object, youâll need to figure out if this method is be called on a create or an update.)
Look at the âhas_â set of methods at The Django admin site | Django documentation | Django. You can override the default has_add and has_change methods in your ModelAdmin class(es) to always return True or False as appropriate.
Thank you dear Mr.Whitsell .
I have understood the answer of my first question .
But about my second question , I have experienced solutions that you said but the problem is that I want to do that for User Model .
And I do not know how to define a model admin for that .
Can you help me with that please ?
You need to:
- Create your âUserAdminâ class inheriting from the systemâs UserAdmin class (
django.contrib.auth.admin.UserAdmin
) - Unregister the default User admin registration
- Register your new UserAdmin class for the User model
Thank you dear Mr.Whitsell .