popup forms

86 views
Skip to first unread message

andreas schmid

unread,
Oct 13, 2009, 3:43:08 AM10/13/09
to django...@googlegroups.com
hi,

how can i achieve a behaviour like in the admin backend where i can add
a related object through a popup window and have it selectable after i
saved the related form?

for example:
im copleting the form book and i have to select the author but it doesnt
exist yet... so i click on the + (add) button and a popup window appears
where i create the editor object, and i can select it in the book form
right after i saved and closed this popup window.

can somebody point me to the code?

thank you in advance...


nabucosound

unread,
Oct 13, 2009, 7:45:44 AM10/13/09
to Django users
This is the default behaviour in Django Admin, dude...

andreas schmid

unread,
Oct 13, 2009, 7:57:10 AM10/13/09
to django...@googlegroups.com
thx mate... but i need it on the front end ;)

Héctor García

unread,
Oct 13, 2009, 8:01:44 AM10/13/09
to django...@googlegroups.com
Oh all right, sorry, I misread first paragraph
--
Hector Garcia - Web developer, musician
NomadBlue URI: http://nomadblue.com/

Andrew Ingram

unread,
Oct 13, 2009, 8:05:33 AM10/13/09
to django...@googlegroups.com
I'm assuming you are doing this somewhere other than the admin, or in
custom views, so I'll explain how the admin stuff works.

Basically, when you create the popup window you give it a name which
can be used the uniquely identify the field that is using the popup
(some variant on the field id would be ideal). Your main page (not the
popup) should also have a javascript function to be called after the
new author is saved (in the case of django admin, this function is
called dismissAddAnotherPopup and is in RelatedObjectLookup.js).

Now the clever part (which I had to hunt around for when I needed this
functionality, you can find it around line 608 in
django.contrib.admin.options.py), is that when you successfully save
the new author, you return an HttpResponse that consists of nothing
but a script tag that executes
owner.yourFunctionName(window_name,new_object_id (in the case of the
django admin this would be owner.dismissAddAnotherPopup), window_name
is the unique identifier you passed in originally.

This causes the browser to execute the function in the owner window
(the one that created the popup) with the parameters you specified -
which includes the ID of the new object. Django's code also provides
the representation string of the object so it can be added to the
select box.

Then you just make your JS function close the popup with window_name.close().

I may not have explained it that well, but the key parts are in
RelatedObjectLookup.js and options.py (near line 608).

I hope this helps.

- Andrew Ingram






2009/10/13 nabucosound <hect...@gmail.com>:

andreas schmid

unread,
Oct 13, 2009, 8:20:41 AM10/13/09
to django...@googlegroups.com
thank you very much for pointing me to the right path!!
ill try to understand the behaviour and report about my progress...

Emily Rodgers

unread,
Oct 13, 2009, 8:45:25 AM10/13/09
to Django users
> > 2009/10/13 nabucosound <hecto...@gmail.com>:
>
> >> This is the default behaviour in Django Admin, dude...
>
> >> On Oct 13, 9:43 am, andreas schmid <a.schmi...@gmail.com> wrote:
>
> >>> hi,
>
> >>> how can i achieve a behaviour like in the admin backend where i can add
> >>> a related object through a popup window and have it selectable after i
> >>> saved the related form?
>
> >>> for example:
> >>> im copleting the form book and i have to select the author but it doesnt
> >>> exist yet... so i click on the + (add) button and a popup window appears
> >>> where i create the editor object, and i can select it in the book form
> >>> right after i saved and closed this popup window.
>
> >>> can somebody point me to the code?
>
> >>> thank you in advance...

You might find this helpful: http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/

Em

andreas schmid

unread,
Oct 13, 2009, 9:03:59 AM10/13/09
to django...@googlegroups.com
that looks like what i wanted... ill take a look at all your tips asap!
thx

andreas schmid

unread,
Nov 30, 2009, 6:40:21 AM11/30/09
to django...@googlegroups.com
the tutorial is nice but i cant get it really working. the problem is
that the popup opens but i get a:

TypeError at /popadd/topics/

'str' object is not callable

Request Method: GET
Request URL: http://127.0.0.1:8000/de/popadd/topics/?_popup=1
Exception Type: TypeError
Exception Value:

'str' object is not callable

Exception Location:
/home/pepe/DEV/FSlabs/parts/django/django/core/handlers/base.py in
get_response, line 92

my views.projects ProjectForm:

class ProjectForm(ModelForm):
topics = ModelMultipleChoiceField(Topic.objects,
required=False, widget=MultipleSelectWithPop)
technologies = ModelMultipleChoiceField(Technology.objects,
required=False, widget=MultipleSelectWithPop)
class Meta:
model = Project
exclude = ['author']


my views.handlePopAdd.py :

from django.utils.html import escape
from django.contrib.auth.decorators import login_required

from myapp.views.technologies import TechnologyForm
from myapp.views.topics import TopicForm


def handlePopAdd(request, addForm, field):
if request.method == "POST":
form = addForm(request.POST)
if form.is_valid():
try:
newObject = form.save()
except forms.ValidationError, error:
newObject = None
if newObject:
return HttpResponse('<script
type="text/javascript">opener.dismissAddAnotherPopup(window, "%s",
"%s");</script>' % \
(escape(newObject._get_pk_val()),
escape(newObject)))

else:
form = addForm()

pageContext = {'form': form, 'field': field}
return render_to_response("add/popadd.html", pageContext)

@login_required
def newTopic(request):
return handlePopAdd(request, TopicForm, 'topics')

@login_required
def newTechnology(request):
return handlePopAdd(request, TechnologyForm, 'technologies')

any suggestions?

Bhaskar Gara

unread,
Jan 28, 2010, 9:05:24 PM1/28/10
to Django users
Hi Andrew, Do you have any luck on this. I need same functionality.

On Nov 30 2009, 5:40 am, andreas schmid <a.schmi...@gmail.com> wrote:
> Emily Rodgers wrote:
>
> > On Oct 13, 1:20 pm, andreas schmid <a.schmi...@gmail.com> wrote:
>
> >> thank you very much for pointing me to the right path!!
> >> ill try to understand the behaviour and report about my progress...
>
> >> Andrew Ingram wrote:
>
> >>> I'm assuming you are doing this somewhere other than the admin, or in
> >>> custom views, so I'll explain how the admin stuff works.
>

> >>> Basically, when you create thepopupwindowyou give it a name which


> >>> can be used the uniquely identify the field that is using thepopup
> >>> (some variant on the field id would be ideal). Your main page (not the
> >>>popup) should also have a javascript function to be called after the
> >>> new author is saved (in the case of django admin, this function is
> >>> called dismissAddAnotherPopup and is in RelatedObjectLookup.js).
>
> >>> Now the clever part (which I had to hunt around for when I needed this
> >>> functionality, you can find it around line 608 in
> >>> django.contrib.admin.options.py), is that when you successfully save
> >>> the new author, you return an HttpResponse that consists of nothing
> >>> but a script tag that executes
> >>> owner.yourFunctionName(window_name,new_object_id (in the case of the
> >>> django admin this would be owner.dismissAddAnotherPopup), window_name
> >>> is the unique identifier you passed in originally.
>
> >>> This causes the browser to execute the function in the ownerwindow

> >>> (the one that created thepopup) with the parameters you specified -


> >>> which includes the ID of the new object. Django's code also provides
> >>> the representation string of the object so it can be added to the
> >>> select box.
>

> >>> Then you just make your JS function close thepopupwith window_name.close().


>
> >>> I may not have explained it that well, but the key parts are in
> >>> RelatedObjectLookup.js and options.py (near line 608).
>
> >>> I hope this helps.
>
> >>> - Andrew Ingram
>
> >>> 2009/10/13 nabucosound <hecto...@gmail.com>:
>
> >>>> This is the default behaviour in Django Admin, dude...
>
> >>>> On Oct 13, 9:43 am, andreas schmid <a.schmi...@gmail.com> wrote:
>
> >>>>> hi,
>
> >>>>> how can i achieve a behaviour like in the admin backend where i can add

> >>>>> a related object through apopupwindowand have it selectable after i


> >>>>> saved the related form?
>
> >>>>> for example:
> >>>>> im copleting the form book and i have to select the author but it doesnt
> >>>>> exist yet... so i click on the + (add) button and apopupwindowappears
> >>>>> where i create the editor object, and i can select it in the book form
> >>>>> right after i saved and closed thispopupwindow.
>
> >>>>> can somebody point me to the code?
>
> >>>>> thank you in advance...
>
> > You might find this helpful:http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/
>
> > Em
>
> the tutorial is nice but i cant get it really working. the problem is

> that thepopupopens but i get a:

> any suggestions?- Hide quoted text -
>
> - Show quoted text -

andreas schmid

unread,
Jan 29, 2010, 2:45:20 AM1/29/10
to django...@googlegroups.com
hi, yes it works perfectly :)

follow the steps on the turorial here

http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/

sometimes its not so clear where to put the code... if you have questions ill try to help you.

ill make a "more complete" tutorial on my blog soon.

Bhaskar Gara

unread,
Jan 29, 2010, 7:20:55 AM1/29/10
to Django users
That where I stuck.. I am new to Django so I confuse where to add
what? what name i need to give to the widget,form etc.,

Can you please help me.

Thank you
Bhaskar

> >> - Show quoted text -- Hide quoted text -

Reply all
Reply to author
Forward
0 new messages