I have been doing CRUD operation in Django but I am having problem with what to do after successful operation.
Is there any way that i can redirect the user to Home page with small message on top of page like
"Record Successfully added or something else" like Google does with Dismiss hyperlink :)
EDIT:
Actually that a is full documentation and not a simple example.
I could not understand in which template I have to include {{messages}} in. In update form, edit form or home page form?
I don't understand where I need to define messages. I use only generic views so I am confused.
Use the django.contrib.messages framework to get the same messaging style as used in Django admin.
Enable the middleware module by adding 'django.contrib.sessions.middleware.SessionMiddleware' to MIDDLEWARE_CLASSES in settings.py.
In your view, use something like messages.add_message(request, messages.INFO, 'Stuff happened') to record information where it makes sense.
In your template, you'll have a list messages which you can iterate over where it makes sense in your design - the example in the documentation shows a full example which exposes the tags as CSS classes for ease of styling.
Related
I'm currently using out-of-the-box django.contrib.auth to handle authentication in my Django app. This means that the user starts at a log in page and is redirected to the app on successful login. I would like to make my app single-page, including this login process, where a redirect doesn't happen, but maybe a "hot" template switch-out or some fancy client-side div magic (that still remains secure). My Google searching turned up pretty short, the closest solution dealing with putting a log in form on every page.
Any direction or ideas here would be much appreciated. I would obviously prefer to work within the existing confines of django.contrib.auth if possible, but I'm open to all solutions.
I'm not sure I understand your question completely. I think you want to have a single page. If so, put logic in your template that checks to see if the user is authenticated. If not, display a login form that POSTS to the appropriate django.contrib.auth view. You can supply an argument to this view to have it redirect back to your page. When you come back, the user will be authenticated, so you won't display the login form.
Have a look at Django-Easy-Pjax https://pypi.python.org/pypi/django-easy-pjax - it works like a charm and is well documented. Everything you like is being made with AJAX requests: links, forms using GET and forms using POST.
Essentially you only need to add a data-pjax="#id_of_the_container_where_the_result_goes" attribute in your a and form tags.
And the great thing about it: It updates the title and location bar of your browser.
One caveat: If you want to upload files in some form, this is not supported by Easy-Pjax, so you might want to use some workaround jQuery library for that.
I am using django-allauth for one of my project. I would like to implement login/signup process via ajax. I would like to have customized signup form. I was going through their signupmixin and signup form. Sounds like I can write custom views for each action and map it to the url config. I am not sure what is the best way to do this.
Thank you so much for any help or advice on this.
It depends a bit on what you mean by ajax. If you just want to have a popup-style login/signup box on every page, then you can simply add the form to your base template and show it dynamically using Javascript in a popup box or so. If you keep the form action url to the original allauth urls then this will already give the feel of an ajax signin. You could also tweak things by using $.ajax or $.post to post to the original allauth views.
Something like the above is done on http://officecheese.com/ -- this is an allauth based site, though I am not affiliated with it.
If by ajax you mean that all authentication related views should be displayed via ajax, without causing a new document reload, then I am afraid you are a little bit out of luck. This simply is problematic for scenario's where e-mail verification, or OAuth handshakes are involed, as here you are typically navigating to a new URL from your mailbox, or redirecting to Twitter and so on.
Is there a good way to have both the login and register forms for django-registration on one page? I've had trouble finding a way to do it now that the backend system is enforced. Is there a view that can be overwritten that would allow you to add both forms to it? Anyone done this before or can point to an article about this?
Edit: Just to clarify I have the whole django-registration and login system set up and working properly, I'd just like to get both forms on the same page. I do not have access to their views.
Just hard-code your login-form in the registration-html-template. It should work like a charm.
You can always override the default login and registration views/templates. You can take a look at this link and see if this was what you were thinking to do. Then, you can read the Django documentation for further information about making custom login and registration views and templates as well.
I have a third party app that requires a template.
I would like to inject a message in the urls.py so the message
is displayed in the template as I don't have access to the view
of the 3rd party app.
How can this be achieved the DRY way?
UPDATE: Trying to give more info regarding this question.
I have django-registration on my site (outsourcefactor.com) and any request starting with "/signup/" redirects to django-registration which is out of my control. I would have wanted to insert a message into django's message framework so I could print the message in the signup template which is under my control.
I thought the only place that I could insert a message would be in the urls.py, which is just before releasing the control to django-registration, then catch the message in the registration template and display it.
I am using django-messaging for my event-drive messages, and didn't want to pass the message via context processor or ..etc.
I am trying to avoid forking and modifying django-registration.
Hope this is more clear now.
You can override any of this django-registration urls with custom template.
Django's auth messages look pretty handy for notifying a CMS user of some configuration problem. The thing is that the messages are deleted on every page load if you include the "django.core.context_processors.auth" context processor, and you have to include that processor if you want to use the admin interface.
I tried hacking around it by adding that processor to TEMPLATE_CONTEXT_PROCESSORS just after matching the admin url / before calling admin.site.root, but it appears that it's already imported the list of processors by that time.
So is there any way to do this without changing any of the Django core files themselves, and without omitting the django Auth app from your config entirely until the last possible moment?
If the Django message system was critical for my site I would add the messaging into my views with, for example:
integration with Growl
some javascript modal dialog like jQuery Alert Dialogs
If you really need to hack it so they only show in admin pages then the easiest solution would be to copy the auth function in django.contrib.core.context_processors.py and put it into a context_processors.py in your own application directory. Use it in the CONTEXT_PROCESSORS stack instead of the django.core version and modify it so it looks at the REQUEST url and only calls user.get_and_delete_messages() if it is the admin url.