Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirecting in a plugin view. #79

Closed
ichernetsky opened this issue Jun 19, 2009 · 20 comments
Closed

Redirecting in a plugin view. #79

ichernetsky opened this issue Jun 19, 2009 · 20 comments

Comments

@ichernetsky
Copy link

Please add the possibility to redirect an user in a plugin view. Sometimes it's preferred to control a flow on the server side, instead of using AJAX.

@digi604
Copy link
Contributor

digi604 commented Jun 20, 2009

maybe a possibility to raise an exception and middleware that will handle it

@thomasst
Copy link

thomasst commented Dec 9, 2009

Right now this does not work as expected, as raising an exception in a plugin raises a TemplateSyntaxError. So no way to do anything.

@digi604
Copy link
Contributor

digi604 commented Dec 10, 2009

we could set a variable on the request and the middleware could look for if this variable is present.

@thomasst
Copy link

This works, but still it would be more convenient if there was the possibility to return a HttpResponse in the plugin, as

  1. it's cleaner and easier
  2. Django doesn't have to process the entire response

Why do we have to return the context anyway, if we can modify the context directly?

@digi604
Copy link
Contributor

digi604 commented Dec 10, 2009

The problem is that at the moment when the plugin is rendered we are not anymore in a view but already in render_to_response. This is needed for 3th party app support. We could check if the render function returns a HttpResponse.... and if it does wrap it like described above... would make the api simpler and more intuitive..

@digi604
Copy link
Contributor

digi604 commented Dec 10, 2009

We also could assign the response to the request and then raise an exception... this would eliminate the render to the end problem.

@digi604
Copy link
Contributor

digi604 commented Dec 10, 2009

What do you mean by: if we can modify the context directly?

@thomasst
Copy link

I did not take a look at the django-cms code, but the context variable is passed to the render method, that's why we could just update it and not return it. That way, the return value of render() would be reserved for a HttpResponse. But I agree that we shouldn't change the behaviour, so a check isinstance(retval, HttpResponse) might be an option.

@samluescher
Copy link
Contributor

Well, maybe we could add an (optional) process(request) method to plugins that does whatever it needs to do – for instance, validate a form – and is called before anything is rendered. That method could return a HttpResponse object or nothing at all or… well, just thinking along.

In any case, it would be really nice if you could redirect the browser after form submissions to prevent double submissions (standard behaviour for many web apps).

@digi604
Copy link
Contributor

digi604 commented Jan 13, 2010

if we would have a second method for processing the request you would need to process it twice for rendering the form in the render method. There is also the question of when to process this as the plugin_media is optional. Anyway I would say that the render method is enough.

The question is should a HttpResponse be returned and then the calling function raise a Exception and the middleware handles it and returns the appropriate response or should render raise an Exception itself. I think returning HttpResponse is more django-like

@jezdez
Copy link
Contributor

jezdez commented Jan 14, 2010

+1 on returning a HttpResponse instance directly.

@benjaoming
Copy link
Contributor

I don't think this behavior is desirable at all. Plugins are for usability. They should not implement any sort of behavioral logic but should be easy to preview in the admin and understand from a users perspective.

We use CMS because content maintainers are not techies.

You cannot preview a redirect.

People who want redirects should implement a view or some middleware. I don't see the justification why this is excluded.

@rcmachado
Copy link

+1 on returning a HttpResponse

@ojii
Copy link
Contributor

ojii commented Sep 17, 2010

-1 on this issue. This is not what plugins are there for.

@thomasst
Copy link

+1

@piotrkilczuk
Copy link

I'd generally second ojii.

I think DjangoCMS needs a BD(FL?), otherwise we will be stuck on issues like this one :)

@ojii
Copy link
Contributor

ojii commented Oct 26, 2010

okay here's the BDFL decision on it:
NO!

(make apphooks for this kind of stuff)

@motiejus
Copy link
Contributor

motiejus commented Sep 6, 2011

A workaround here:
http://djangosnippets.org/snippets/2541/

@Lacrymology
Copy link

+1 on @motiejus 's snippet. Sometimes (like after a form POST processing) you need to (or even worse, should) redirect.

@o3bvv
Copy link

o3bvv commented Nov 11, 2014

This does not work any more, because error is raised during rendering of responce, i.e. here:

https://github.com/django/django/blob/master/django/core/handlers/base.py#L157

And it is not wrapped with try...except, and no middleware exception handler is called 😞

yakky pushed a commit that referenced this issue Mar 24, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests