Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » add template variable to an external view, without changing the view [RSS Feed]

#1 Dec. 20, 2010 15:00:39

y.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

add template variable to an external view, without changing the view


Hello

I'm using the registration django app and want to have the
registration and login form on the same page.
When I reuse the auth_login view, the registration form is missing in
the context of the template.
My quickfix is to copy the original auth login view to my own views.
Then I changed the the call of "return render_to_response(..." and
added the registration form. This works but this approach is not very
DRY.

I tried other approaches, but nothing worked.
The first idea was to use a view decorator, but this don't worked
because the view decorator
is to late in the chain and already has the finished rendered template
html code.

The next idea was to import the login view and than to redefine the
render_to_response function. I only got this working when the login
view code is in my own views file, so not very DRY.

I tried to make a new "version" of the login view with the python new
module. The idea was to pass
a new global context to the login view so that it uses my changed
render_to_response function.

This was the code:
loign_new = new.function(login.func_code, globals(), login.func_name,
login.func_defaults, login.func_closure)

It worked partial, put the registration form was the same as login
form, so some fields were missing.

In django 1.3 there are are class based view, which solves this issue
with the function get_context_data, but the external app must uses
class based views, so the future looks birght :-)

Is there any solution to get this working in django 1.2 with views
from other apps? Any dirty python tricks?

regards
Yves

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 20, 2010 15:31:37

Mikhail K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

add template variable to an external view, without changing the view


Hi Yves,

In django 1.3 there is also a TemplateResponse (http://docs.djangoproject.com/en/dev/ref/template-response/) that can
perform as render_to_response replacement, doesn't require class based
views and enables developer to change the template context (as well as
the template to be rendered) of a third-party view so the future is
even brighter ;)

It still requires changes to third-party app but the changes are quite
trivial (replacing render_to_response with even simpler
TemplateResponse call) and I expect third-party apps to begin
replacing render_to_response with TemplateResponse or with class based
views once django 1.3 will be released.

In case of registration and login form at the same pages, there are 2
viable solutions:

1. add the login form to context in the context processor (globally or
based on request url);
2. write the html without constructing the form object. This object is
not really necessary because the validation, etc. should be handled by
an another view and django form object is not necessary to make html
form in django.

On 20 дек, 20:00, yves_s <yves.serr...@gmail.com> wrote:
> Hello
>
> I'm using the registration django app and want to have the
> registration and login form on the same page.
> When I reuse the auth_login view, the registration form is missing in
> the context of the template.
> My quickfix is to copy the original auth login view to my own views.
> Then I changed the the call of "return render_to_response(..." and
> added the registration form. This works but this approach is not very
> DRY.
>
> I tried other approaches, but nothing worked.
> The first idea was to use a view decorator, but this don't worked
> because the view decorator
> is to late in the chain and already has the finished rendered template
> html code.
>
> The next idea was to import the login view and than to redefine the
> render_to_response function. I only got this working when the login
> view code is in my own views file, so not very DRY.
>
> I tried to make a new "version" of the login view with the python new
> module. The idea was to pass
> a new global context to the login view so that it uses my changed
> render_to_response function.
>
> This was the code:
> loign_new = new.function(login.func_code, globals(), login.func_name,
> login.func_defaults, login.func_closure)
>
> It worked partial, put the registration form was the same as login
> form, so some fields were missing.
>
> In django 1.3 there are are class based view, which solves this issue
> with the function get_context_data, but the external app must uses
> class based views, so the future looks birght :-)
>
> Is there any solution to get this working in django 1.2 with views
> from other apps? Any dirty python tricks?
>
> regards
> Yves

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » add template variable to an external view, without changing the view [RSS Feed]

Board footer

Moderator control

Enjoy the 17th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support