Python/Django - initial/default admin page - python

After a user has logged into the admin interface is it possible to redirect them to a specific page?
I want to redirect to the list of entries for a particular model.
I'm using Django 1.3
Cheers!

You can set the default redirect url (on login) in your settings.py
LOGIN_REDIRECT_URL = "/admin/mymodel/"
https://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#login-redirect-url

Look at ticket 14510 .
Seems django admin doesn't support LOGIN_REDIRECT_URL settings and you should subclass AdminSite.

Related

How do I redirect from a view to a root URL in Django?

I'm trying to add a link to my django site that redirects from the catalog1 view to the catalog2 page. So basically, I want to go from .../catalog1 to .../catalog2/other-page.
All the other answers that I've seen require redirecting to http://127.0.0.1:8000/ first, and then redirecting to catalog2, but I've set up the site so that http://127.0.0.1:8000/ automatically redirects to catalog1, so this isn't working.
Assume, a situation , your other-page is about,you have about button in your path http://127.0.0.1:8000/catalog1
catalog1.html
about
urls.py
urlpatterns =[
path('catalog2/about/',views.about,name='about')
]
views.py
def about(req):
return render(req,'appname/anyfile.html')
So, by clicking on a button about, it will redirect it to that view which has name='about' in urls.py irrespective of any route.
Then you are redirected to http://127.0.0.1:8000/catelog2/about/ from http://127.0.0.1/8000/catalog1.

django admin remove login page

Is there anyway to delete django admin login page (mySite.com/admin) and use the user session which has logged in in main site (mySite.com)?
If any code is needed please tell me to add.
My middleware in settings.py is:
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
.
.
.
.
.
]
update:
the reason is I want the admin first logs in with his account in website then open admin page. Other users would see admin link, but after clicking that they would see a message you don't have permission to see or change anything and they see nothing else. I just want myWebsite.com/admin be redirected to admin:index if the user is logged in and to myWebsite.com if he is not.
You can easily do this using your main urls.py file. Just redirect the admin login URL to your custom log-in page on your website. In the below sample you will notice the normal admin.site.urls being used AFTER admin/login and admin/logout (I assume you'll also have a custom log-out page). With these custom views being first they will take precedence and be used instead of the ones in admin.site.urls.
urlpatterns = [
url(r'^admin/login', your_custom_login_view, name='custom_login_page_admin'),
url(r'^admin/logout', your_custom_logout_view, name='custom_logout_view_admin'),
url(r'^admin/', admin.site.urls),
]
Create a custom subclass of AdminSite and overwrite the login() method. Something like this:
class CustomAdminSite(admin.AdminSite):
def login(self, request, extra_context=None):
if not request.user.is_authenticated:
# not authenticated, redirect to main login page
login_path = reverse('login')
return HttpResponseRedirect(login_path)
if self.has_permission(request):
# Already logged-in, redirect to admin index
index_path = reverse('admin:index', current_app=self.name)
return HttpResponseRedirect(index_path)
else:
# Logged in, but doesn't have required permissions
return render(...) # render a template with your error message
Follow the Django documentation on how to customize the AdminSite class.
Why you want to do that? Admin Page is for admin purposes, just dont access /admin/ path anymore or just remove admin from your urls.py, doign that you will not be able to access admin pages anymore...
But if you want to make your users access your django admin native pages using your custom login page, just make sure to tag your users with is_staff so they can access native django pages...
models.py
from django.contrib.auth.models import User
class CustomUser(User):
... # Your new fields
views.py
def create_user(request):
...
user, created = CustomUser.objects.get_or_created(
... # Your Custom + User Django fields
is_staff = True # This will allow this user to access the admin page
)
If you want allow all your users to access your django admin pages without need to use Django Login Page you can override your CustomUser model to set all users with is_staff
class CustomUser(User):
... # Your new fields
def save(self, *args, **kwargs):
if not self.id: # This indentify if the registry is new...
self.is_staff = True
super(CustomUser, self).save(*args, **kwargs)
Obs.: Make sure your User models extends the Django User Auth, there 2... one more complex and one simple, check the docs
https://docs.djangoproject.com/en/2.0/ref/contrib/auth/
All mentioned solutions here might do a good job, but my solution would be a little different: If you want to redirect to your default login page and hide the django-admin, sth. simple like this is a good workaround:
urlpatterns = [
path('admin/login/', RedirectView.as_view(pattern_name='account_login', permanent=True)),
]
Of course the target route (here account_login) can be changed as desired.
I prefer this approach:
on urls.py
from .settings import LOGIN_URL
from django.views.generic import RedirectView,
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
path('admin/login/', RedirectView.as_view(url=LOGIN_URL)),
path('admin/', admin.site.urls),
....
]
To disable admin just remove url(r'^admin/', admin.site.urls), from your main urls.py. Another things to clean are 'django.contrib.admin' from INSTALLED_APPS.
A real easy way to accomplish this is by doing 2 things:
Head to your settings.py file and add: ADMIN_ENABLED = False
Head to your main urls.py file which should have something that looks like:
from django.urls import path, include
urlpatterns = [
# path('admin/', admin.site.urls),
path('', include("landing.urls")),
]
and comment out the path to admin like in the above snippet.
Cheers!

Django 1.8 not using my custom login page

I am working Django 1.8 and having trouble using my custom login page. Whenever I go to my login url, the Django admin username and password form comes up instead of my custom login.html.
I have worked in the Django 1.7 and this works fine but I do not know why I am having the error in 1.8.
login.html is my custom login template.
My urls:
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}, name='auth_login'),
I have checked the forums and the Google but still cannot find a similar error.
EDIT:
Answer thanks to Kishor.
In Django 1.8 I simply renamed the admin pages that I was using, namely, base.html, base_site.html and login.html [I store these files inside a folder called 'admin' which is inside my app's 'templates' folder]
Instead of using the normal way like in previous versions of Django i just called the three pages above myapp_base.html, myapp_base_site.html and myapp_login.html [Do not forget to also change the tag at the top inside these pages where is says 'extends']
From there my app was able to pick up that I wanted to override the standard Django login page and customize it to my specification.
Use this 1.8 way/generic view way
url(r'^accounts/login/$', auth_views.login, {'template_name': 'myapp/login.html'}),
and be sure that you have included
url('^', include('django.contrib.auth.urls'))

How to redirect people after they register using django registration

url(r'^register/$',
RegistrationView.as_view(form_class=CustomRegistrationForm,
success_url='/profile'),
name='registration_register',
success_url='/profile'),
I have the following in my urls.py.
How do I redirect people after they register using django registration 1.0?
The redirect will be issued to the path specified by RegistrationView.get_success_url, which should be RegistrationView.success_url which you've specified when constructing the view function.
You can review RequestView.form_valid for more details.

how to set the admin user with GAE dev app server?

I want to create an Admin-only page for my GAE application. But I found there's no way to set a Admin user, so I cannot log on the page I created and test the function. I tried the local Admin console, but no luck. How can I do this?
Google App Engine provides a pretty straightforward way to create a private admin section of your web application.
1.
In your app.yaml, any URL handler can have a login setting to restrict visitors to only those users who have signed in, or just those users who are administrators for the application.
If the setting is login: admin, once the user has signed in, the handler checks whether the user is an administrator for the application. If not, the user is given an error message; if the user is an administrator, the handler proceeds.
Here a snippet of app.yaml where the /admin/.* routes are admin restricted :
- url: /admin/.*
script: admin.py
login: admin
2.
Trying to access the admin url, the dev app server automatically shows the login panel where you should check the Sign in as Administrator checkbox.
When you log in (the blue box where you enter the email) there is a checkbox to apply the administrator flag to your session.
You can define admin only pages in your app.yaml, like so:
- url: /secrets/
script: /secrets/example.py
login: admin
an admin is whoever is so defined in your appengine.google.com control panel, under permissions.

Categories

Resources