Caveat to that, is there a way to have to SQLFORM top and bottom like the
picture attached?
Its more like a django kind of admin panel feature. But in web2py, can we
have the post and comment (two tables), embedded so when someone click on
the blue "plus" sign a comment box shows up and people insert the comment
on each post. Its one to many relationship (like attached picture).
If someone really trying to know what I mean by that, here is the class
definition generated for django.
class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
title = models.CharField(max_length=255)
slug = models.SlugField()
body = models.TextField()
comments = models.ManyToManyField('Comment')
def get_absolute_url(self):
return reverse('post', kwargs={"slug": self.slug})
def __unicode__(self):
return self.title
class Meta:
ordering = ["-created_at"]
class Comment(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
body = models.TextField(verbose_name="Comment")
author = models.CharField(verbose_name="Name", max_length=255)
By the way, this app thats call "web2py.app.dummy.w2p", please be aware
that it has some nasty nested stuff in it. Once I installed it was crawling
all over desktop and wouldn't uninstall except from the DOS mode. Not fair.
On Thursday, May 14, 2015 at 3:53:15 PM UTC-4, LoveWeb2py wrote:
>
> No problem, Richard. Thank you for your help!
>
> On Thursday, May 14, 2015 at 11:45:28 AM UTC-4, Richard wrote:
>
> No time to hunt the issue with multiples loading...
>
> I guess it could come from not using the same web2py version... Or because
> I made some change over app files which are binded to web2py version
> /statis/js/web2py.js for instance... or views/web2py_ajax.html
>
>
> You have at least an semi-working example... There is 2 issues that remain
> : 1) The component load the whole site which he shouldn't (that may be
> related to the second issue), 2) When you access the
> http://hostname/appname/second_table_form/second_table there is a weird
> issue with LOAD...
>
> I may have more time next week, but for now, I can't help you further...
>
> Richard
>
>
> On Tue, May 12, 2015 at 11:11 AM, LoveWeb2py <[email protected]> wrote:
>
> Repeating load might be because I extended the layout in the view for the
> load.
>
> On Tuesday, May 12, 2015 at 10:22:41 AM UTC-4, Richard wrote:
>
> Almost there... I make it works, but there is something weird going on
> related to component... I have to figure it out... But you were doing thing
> the wrong way basically... add_function is for the main_table which
> contains the city name...
>
> I will send you back the corrected app when I found the issue with the
> repeating load...
>
> Richard
>
> On Mon, May 11, 2015 at 4:39 PM, LoveWeb2py <[email protected]> wrote:
>
> No rush, Richard. Thank you for your help I really appreciate it.
>
> On Monday, May 11, 2015 at 4:34:14 PM UTC-4, Richard wrote:
>
> I will try to review it ASAP...
>
> On Mon, May 11, 2015 at 4:30 PM, LoveWeb2py <[email protected]> wrote:
>
> Here is my dummy app, Richard.
>
>
> On Monday, May 11, 2015 at 3:50:20 PM UTC-4, Richard wrote:
>
> Did you make dummy app for testing it?
>
> Please attach it here and I have a look to your code... I can't help much
> if I don't have a good idea of what you have done...
>
> new_exp_num=form.vars.exp_num
>
> This is something I left which should be adapted to your app...
> form.vars.exp_num is the representation of the new inputed record... The
> same field name you specify in the widget declaration for the field
> attribute : db.table.representing_field, but that you get from the form
> of the referenced table.
>
> Richard
>
>
> On Mon, May 11, 2015 at 3:28 PM, LoveWeb2py <[email protected]> wrote:
>
> Thanks haha. Just checking. Still not having much luck. I'm getting a 500
> error on the add function and can't figure out why
>
> when I try to edit my site I am getting an error for the "add function"
> specified to add the
> https://www.mywebsite.com/add_function/second_table_field
> def add_function():
> """
> Modal form for adding element
> """
>
>
> buttons = [TAG.button((I('', _class='icon-ok icon-large icon-white'),
> CAT(' '), STRONG(T('Add'))),
> _type='submit',
> _class='btn btn-small btn-primary',
> _id='add_button_id',
> _name='add_button',
> )]
> form = SQLFORM(db.table, buttons=buttons, formstyle=formstyle,
> separator=separator)
> response.js = '$(document).ready(function(){
> $(".input_wrapper").has(".error").addClass("inputError"); ' \
> '$(".w2p_fw").has(".error").addClass("control-group
> error"); ' \
> '$(".w2p_fw").each(function(){
> $(this).find(".error_wrapper").appendTo(this); }); });'
> response.js += '$(document).ready(function(){
> $("textarea").elastic(); });'
> response.js += '$(document).ready(function () {
> $("[rel=tooltip]").tooltip(); });'
> if form.process(formname='add_form').accepted:
> response.flash = T("Added")
> target = request.args(0)
> # close modal
> response.js = '$("#{target}_modal-form").modal("hide");'.format(
> target=target)
> # update the select options
> response.js += """$("#{target}").append(
> "<option value='{new_id}'>
> {new_id}
> </option>");""".format(target=target, new_id=
> form.vars.id)
> # select newly added option
> response.js += """$("#{target}").val("{new_exp_num}");
>
> $('#{target}').keyup(); $('#{target}').blur();
> """.format(target=target,
>
> new_exp_num=form.vars.exp_num)
> elif form.errors:
> response.flash = ''
> else:
> response.flash = ''
> return dict(form=form)
>
> Do I need to actually specify the db.table? From the looks of the class I
> thought it would be defined on its own... am I wrong?
>
> Sorry for asking so many questions. I think this will be really helpful
> when it's all finished.
>
>
>
>
> On Monday, May 11, 2015 at 2:59:21 PM UTC-4, Richard wrote:
>
> The commented lines in the class was there because I recently refactor it
> to make it use boostrap modal instead of jquery ui dialog as you may
> understand carefully reading the code!!
>
> :-P
>
> Richard
>
> On Mon, May 11, 2015 at 2:40 PM, LoveWeb2py <[email protected]> wrote:
>
> Are the commented lines necessary or was that for self reference?
>
> # js += '$("#%s_option_add_trigger").
> click(function() {' \
> # ' $("#%s_dialog-form").dialog("open");' \
> # ' return false;' \
> # ' }); ' % (my_select_id, my_select_id)
>
>
>
> On Thursday, May 7, 2015 at 9:33:28 AM UTC-4, Richard wrote:
>
> you need bootstrap 2.3.2 (not tested with BS3, but I am pretty sure it is
> not working, too differents)...
>
> Did you try it with a recent web2py setup? I would suggest you try with
> web2py 2.9.5 which is what I use actually and it works...
>
> Richard
>
> On Thu, May 7, 2015 at 9:20 AM, LoveWeb2py <[email protected]> wrote:
>
> Another observation:
>
> All of this code:
>
>
>
> js += '$(function() {' \
> ' $("#%s_option_add_trigger").button({' \
> ' text: true,' \
> ' icons: {' \
> ' primary: "ui-icon-circle-plus"' \
> ' }' \
> ' });' \
> '});' % (my_select_id)
> js += '$(function() { ' \
> ' $( "#%s_option_add_trigger" ).css("margin-left",
> "+=5"); ' \
> '});' % (my_select_id)
> js +=
> '$("#{modal_id}").appendTo("body");'.format(modal_id=my_select_id
> + "_modal-form")
>
>
> Get's commented out when it's rendered so it ends up looking like this
> <!-- $(document).read(function() .... .appendTo("body");}); //-->
>
> Any idea why that could be happening and would that impact the button
> showing the modal
>
> On Thursday, May 7, 2015 at 8:45:49 AM UTC-4, LoveWeb2py wrote:
>
> Hi Richard!
>
> I think I'm getting close. No more errors, and I applied the widget to the
> field successfully. I see the + sign next to my field, but nothing happens
> when I click it so I'm going over the javascript with a fine tooth comb. Is
> there any additional .js or .css files I need to include?
>
> This is really looking promising!
>
> On Wednesday, May 6, 2015 at 12:42:34 PM UTC-4, Richard wrote:
>
> Let say the FK field is field2 :
>
> Field('field2','reference main_table', # Better syntax thand
> "db.main_table"
> widget=AutocompleteWidgetSelectOrAddOption(
> request=request,
>
> field=db.main_table.field1, # which is you reprensting field
> id_field=db.main_table.id,
> limitby=(0, 10), # how
> much records you want in the dropbox
> min_length=6, # when you
> want the autocomplete pop in or how many caracters user have to entered
> before the autocomplete kickin
> form_title=T('Add new
> title'),
> controller="controler name
> that contains the below function",
> function="your add
> function name",
> button_text=T('Add new'),
> placeholder=T('Start
> typing...'))
>
> Richard
>
> On Wed, May 6, 2015 at 12:34 PM, LoveWeb2py <[email protected]> wrote:
>
> Thank you, Richard. I think i'm getting closer. Could you show me an
> example of how you apply this to a specific field in SQLFORM?
>
> Going back to my original model:
>
> db.define_table('main_table',
> Field('field1','string'),
> Field('field2','string'),
> migrate=False)
>
> db.define_table('second_table',
> Field('field1','db.main_table'),
> Field('field2','db.main_table')
> migrate=False)
>
> How could I integrate your widget with field 2 for a SQLFORM?
>
>
>
>
>
>
> On Wednesday, May 6, 2015 at 9:40:02 AM UTC-4, Richard wrote:
>
> The id field and what field you want to show as a representation of the id
> field of the referenced table...
>
> Start by putting the widget class in model file even if it not a good
> thing to see if you can make it works...
>
> In the module I have these imports :
>
> from gluon.html import *
> from gluon.sqlhtml import *
> from gluon import current
> from gluon.compileapp import LOAD
>
>
> Which may not be related to the widget class...
>
> Richard
>
>
> On Tue, May 5, 2015 at 4:28 PM, LoveWeb2py <[email protected]> wrote:
>
> Hi Richard,
>
> I'm trying to get this setup, but am receiving an error self.url =
> URL(args=request.args)\nNameError: global name \'URL\' is not defined\n'.
> I'm wondering if this is because the gluon hasn't been imported yet.
>
> Also, could you ellaborate more on this?
>
>
> field=db.table.representing_field,
> id_field=db.table.id_field,
>
> id_field should be the foreignkey table if I am not mistaking?
> db.table.representing field, and db.table2.id_field (with foreign key) does
> that sound right?
>
>
> On Wednesday, April 29, 2015 at 10:04:35 AM UTC-4, Richard wrote:
>
> *# In a modules :*
>
> class AutocompleteWidgetSelectOrAddOption(object):
> _class = 'string'
>
> def __init__(self,
> request,
> field,
> id_field=None,
> db=None,
> orderby=None,
> limitby=(0, 10),
> keyword='_autocomplete_%(fieldname)s',
> min_length=2,
> #
> -------------------------------------------------------------
> # From : SelectOrAddOption
> controller=None, function=None, form_title=None,
> button_text = None, dialog_width=1000,
> #
> -------------------------------------------------------------
> placeholder=None
> ):
> self.request = request
> self.keyword = keyword % dict(fieldname=field.name)
> self.db = db or field._db
> self.orderby = orderby
> self.limitby = limitby
> self.min_length = min_length
> self.fields = [field]
> self.placeholder = placeholder
> if id_field:
> self.is_reference = True
> self.fields.append(id_field)
> else:
> self.is_reference = False
> if hasattr(request, 'application'):
> self.url = URL(args=request.args)
> self.callback()
> else:
> self.url = request
> #
> ----------------------------------------------------------------------
> # From : SelectOrAddOption
> if form_title is None:
> self.form_title = current.T('Add New')
> else:
> self.form_title = current.T(form_title)
> if button_text is None:
> self.button_text = current.T('Add')
> else:
> self.button_text = current.T(button_text)
> self.dialog_width = dialog_width
>
> self.controller = controller
> self.function = function
> #
> ----------------------------------------------------------------------
>
> def callback(self):
> if self.keyword in self.request.vars:
> field = self.fields[0]
> rows = self.db(field.like(self.request.vars[self.keyword]+'%')
> ).select(orderby=self.orderby,
> limitby=self.limitby, *self.fields)
> if rows:
> if self.is_reference:
> id_field = self.fields[1]
> raise HTTP(200, SELECT(_id=self.keyword,
> _class='autocomplete',
> _size=len(rows),
> _multiple=(len(rows) == 1),
> *[OPTION(s[field.name],
> _value=s[id_field.name],
> _selected=(k == 0))
> for k, s in
> enumerate(rows)]).xml())
> else:
> raise HTTP(200, SELECT(_id=self.keyword,
> _class='autocomplete',
> _size=len(rows),
> _multiple=(len(rows) == 1),
> *[OPTION(s[field.name],
> _selected=(k == 0))
> for k, s in
> enumerate(rows)]).xml())
> else:
>
> raise HTTP(200, '')
>
> def __call__(self, field, value, **attributes):
> #
> ----------------------------------------------------------------------
> # From : SelectOrAddOption
> my_select_id = '%s_%s' % (field._tablename, field.name)
> # 'test_ph_eregistry_id' #select_widget.attributes.get('_id', None)
>
> add_args = [my_select_id]
> # create a div that will load the specified controller via ajax
> # form_loader_div = DIV(LOAD(c=self.controller, f=self.function,
> args=add_args, ajax=True),
> # _id=my_select_id + "_dialog-form",
> _title=self.form_title)
> form_loader_div = DIV(DIV(BUTTON('x', _type='button',
> _class='close',
> **{'_data-dismiss': 'modal',
> '_aria-hidden': 'true'}),
> H3(self.form_title, _id='myModalLabel'),
> _class='modal-header'),
> DIV(LOAD(c=self.controller, f=self.function,
> args=add_args, aj
>
> ...
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.