django - Howto inject a message in urls.py - django

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.

Related

How can I do a redirect from a plugin in Django CMS?

I have a form in a plugin that on submit I need to redirect to another page.
What's the best way to accomplish this?
So possible solutions are:
Use an App-Hook
Throw an exception in your plugin render method that would be caught by a middleware class and do the redirect from there.
Create a middleware class and during the "process_response" method check for a value on the request object that was added during the render method of the plugin then do the redirect.
Plugins are not really suitable for processing POST requests and there's no way to influence the HTTP Response object from a plugin (other than the content of that response).
The reason Plugins have no hook to process POST requests is that a single page is usually composed of several plugins, and figuring out which plugin should handle the POST request would be very hard. For the same reason they can't change the response, since two plugins could try to change the response in an incompatible fashion.
The solution for this is to have a dedicated POST endpoint for your plugins (either static via urlpatterns or via an Apphook). That endpoint would then redirect to another page, or the page the form is on, so the plugin would send some data with it. Alternatively the plugin submits the form via AJAX to that endpoint and redirects/acts in javascript.

Implementing Ajax requests / response with django-allauth

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.

how to use django_csrf for mobile application

I am writing a mobile application for a django website. i understand that every form in django has a CSRF token key for protection. when use browser to navigate the site, the server render a key for the user.
What i am confused is for mobile application, we dont need view the presetation layer from the site. I just wanna do a HTTP post to send data. I know i can use csrf_exempt to disable the csrf for that form. or i can make another view to render the csrf token for me, but this way i need extra parsing and http request. so is there a nicer way to do it?
Thanks for your time
If your mobile app is rendering a template you can add {% csrf_token %} into the template that renders the form. If you're not using a form and instead just posting data you can create the token as above and then simply post it's value with the data. And if you're not using a template to create the mobile app's markup well then use csrf_exempt (if say you're just posting data to the server periodically).
Obviously there has to be a view to process the posted data, but even if you're using a generic view for that you could still wrap that view (in your urls.py for example) and gain the use of csrf_exempt

Whats the best way to display message after CRUD operation In Django

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.

How to show Django auth messages only in admin interface?

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.