I'm using Django admin and I'm looking for showing a message to user while my page is loading (something like: please wait...). Is there a way to show this temporary message if the user clicked 'admin:appname_modelname_changelist' link (load an object to change).
I'm not using ajax to transfer the request.
I'm looking for something like ajaxStart and ajaxComplete functions in order to control start/end loading page.
You could override the admin template and add a Javascript onClick handler on the link to show a loader (with jQuery for example).
Overriding admin templates:
https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#overriding-admin-templates
jQuery onClick handler:
https://api.jquery.com/click/
Related
My problem is the following: i have a simple web app that requires a user to upload a file, submitting a POST request to the flask server.
If the file does not match the the requirements set by the application, the user is redirected to the same html page containing a form, and a message warning the user of their mistake is flashed onto the html page.
My problem is the following: currently the form is located at the bottom of the html page, but when the user tries uploading an unsuitable picture, they are redirected to the beginning of the page.
How do I tell flask to redirect the user to a specific section of the page (ie the section where the form is located in?)
you can try use id in your html simply add an id to the form then in your url add an #myFormId, if you'r using url_for do this
redirect(url_for("index")+"#myFormId")
also check this too.
I need to force the user to be authenticated when accessing a static page as part of the website module so far what I've done is this:
class RestrictAreas(Home):
#http.route(['/page1','/page2'],type="http", auth='user', website=True)
def test(self):
website_page = request.env['ir.http']._serve_page()
return website_page
it works for page1 and page2 but I need it to be as dynamic, so that controller intercepts any HTTP request for a web page and validates the user.
Can I use a regex for a slug or something similar?
I am overriding the right method in the controller?
thanks in advvance.
you can overwrite _serve_page() function in ir.http and check if the user is logged in or redirect to login page you don't need to build a custom controller for it, this will handle any static page request and you can build another function for your custom controllers
I have created a working Django application. I am using django-allauth for implementing social-account authentication.
Now, suppose I am logged-in inside my application using an e-mail id whose user does not have a staff access and, if I open admin login page directly, the admin login page is displayed as follows:
My question is: how can I stop Django from displaying the message "Successfully signed in as ... "? Where is the source of this message present?
The Successfully signed in message is a message from django-allauth. It uses the messages framework.
I suggest that you add the template code to display messages to your base template. That way, the message will be displayed immediately after you have logged in, rather than when you go directly to the admin login (which does include the template code to display messages).
The text you are referring to (You are authenticated as ...) can be found in this template:
python3.6/site-packages/django/contrib/admin/templates/admin/login.html
You can override this template to remove the messsage. For example, look into this question for how to override djangos default templates.
To override this you need to inherit the template by {% extends "admin/login.html" %}
Then you need to override the block with the name.
{% blocktrans trimmed %} You are authenticated as {{ username }}, but are not authorized to access this page. Would you like to login to a different account? {% endblocktrans %}
Now you can customize this particular line and then point your function to load your custom html file instead of the standard admin ones or you can directly edit the login.html in your django package(Not a good idea). To know where it is fetching from you can do the following...
$python
>>>import sys
>>>sys.path = sys.path[1:]
>>>import django
>>>print(django.__path__)
And then go into contrib\admin\templates\admin and edit the login.html manually.
I have a Django view that I want to render in a new window when it is called from a python script outside of views.py. My questions are 1.Can I use a Django view from a python script in a subdirectory of my project that is outside the app where the view is located (view location: MyProject/Myapp/views.py and the python script location: MyProject/ProcessingCode/myscript.py)? 2. If that is possible, how do I render the view in a new window?
Django view that I want rendered in a new window:
def Error_Popup_Page(request,message):
context = {'message':message}
return render(request,'InterfaceApp/Error_Notification.html',context)
How it will be used:
def SomeOtherPythonFunction():
try:
#data processing code
except Exception as e:
return Error_Popup_Page(request,'error message')
You're trying to trigger a change on the user browser from on an event that happens on your server. This is impossible to do with Django alone, since Django job is only to respond user requests.
This is where Django is placed in the request chain:
User browser (click on href or ajax event) --> Your server --> Django
--> User browser (gets response and display to the user)
As you can see, there is no way for Django to trigger any event on the user end without the user calling it.
What you're trying to do can be achieved with an async task: in other words, you have to open a websocket and connect the user browser to it with JavaScript.
There are many frameworks for doing this, Celery being the most used in conjuction with Django.
nodejs is also widely used but integrating it with Django is more complex.
I'm not sure whether this is a problem with django-stronghold or with django itself.
What happens:
Calling any front end URL (e.g. http://www.myapp.com/careers) leads me to the login page
The URL shown looks like this: http://www.myapp.com/accounts/login/?next=/careers
Upon login, I am forwarded to the admin interface (like when logging in at http://www.myapp.com/admin)
The displayed URL is still http://www.myapp.com/accounts/login/?next=/careers
What I want:
Upon login, I want to be forwarded to the URL called originally, as correctly stored in the next parameter (in the example: http://www.myapp.com/careers)
My setup:
I'm using Django 1.6.
As suggested in the docs, I have installed django-stronghold via pip and activated it in my project like so:
INSTALLED_APPS = (
...
'stronghold', # last entry
)
MIDDLEWARE_CLASSES = [
...
'stronghold.middleware.LoginRequiredMiddleware', # last entry
]
At first, I ran into a redirect loop, so I defined the STRONGHOLD_PUBLIC_URLS:
STRONGHOLD_PUBLIC_URLS = (
r'^/admin.*?$', # Don't touch the admin pages
r'^/accounts/login/$', # Avoid redirect loop
)
What I tried:
Explicitly setting LOGIN_URL and LOGIN_REDIRECT_URL, as suggested here - didn't help.
In the context of logging into the admin interface, django's login mechanism works fine, just like it is documented here:
Here’s what django.contrib.auth.views.login does:
If called via GET, it displays a login form that POSTs to the same URL. [...]
If called via POST with user submitted credentials, it tries to log the user in. If login is successful, the view redirects to the URL specified in next. [...]
If I call an admin URL other than .../admin (e.g. http://www.myapp.com/admin/myapp_base/event/), the displayed URL remains the same while the login form is shown (no next parameter). After logging in, I see the requested page.
Ideas?
Any ideas why the redirect isn't working for the front end URLs?