How to change view/edit pages of the smartgrid component in web2py - customization

Smartgrid component in web2py is very powerful. I wonder if it is possible to add any additional markup into the View/Edit pages of the smartgrid.
Normall, in web2py we need to create a view html file corresponding to a function in the controller. The problem with smartgrid is that the controller functions are automatically defined by the component.
For example, clicking the View button in a smartgrid goes to the following url:
default/index/dataset/view/dataset/1
Now, my question is if I can create a custom view html file for this page that can contain things other than smartgrid?

The smartgrid component is not automatically defining controller functions. Rather, the links for view, edit, etc. are simply passing additional arguments to the same function where the smartgrid is defined (e.g., in the above URL, dataset/view/dataset/1 are all arguments to the index function, which presumably is where your smartgrid is defined).
You have at least two options. First, you could add conditional logic to the index.html view, such as:
{{if 'view' in request.args:}}
[special code for viewing a record]
{{else:}}
[regular grid view code]
{{pass}}
Alternatively, you could specify a different view from within the controller function, such as:
def index():
if 'view' in request.args:
response.view = 'default/view_record.html'
[rest of index code]

Related

Set URL for Submit Page in Controller Rails

So I have this url in my site
localhost/home-loan
And when clicked, it performs the following function from the controller page
mortgage_loans_controller.rb
I then fill up a form and click submit. But i want my submit page to have the following url,
localhost/home-loan/thank-you
But it has localhost/mortgage_loans/thank_you
thank_you is also another function in the mortgage_loans_controller.
How can i do this?
Use this:
match '/home-loan/thank-you', to: 'your_controller#your_action', as: 'custom_name_for_helper', via: [:post]
Place this line inside of the routes.rb.
This line says that whenever you doing POST request to the localhost/home-loan/thank-you, execute your_controller's your_action.
as: options will use 'custom_name_for_helper' to create path helper, like custom_name_for_helper_path.
via: option determines which request method used, if you need to handle all types just use via: :all
I don't know the structure of your routes.rb, so place this line above all routes, to be sure that it will work.

Dynamically create view

I'm generating a list of views that the client should cycle through on the server. The server returns a list of something like 'App.AView', 'App.BView', 'App.CView", ..., etc. that refer to views and templates on the client.
I'd like to dynamically create these views, swap out the previous view, and include the new view. My first though was to compile a handlebars template with this dynamic view name, ala:
App.QuestionView = Em.View.extend({
template: function() {
return Ember.Handlebars.compile("{{view " + this.get("view_name') + "}}");
}
});
Which works, but seems ugly - is there a way to create a view with a string of the view name in Ember.JS and replace an existing view in a parent view with that view?
You want to look at Ember.ContainerView, which will let you programmatically manage a set of child views. Container View's documentation is excellent, check out: http://emberjs.com/api/classes/Ember.ContainerView.html
Since it sounds like you only ever want one view displayed at a time you can focus on the currentView property, which will automatically maintain your childViews array for displaying a single view.
Also here is an example: http://www.emberplay.com/#/workspace/2792969430

How to make a dynamic menu in base template using django

I'm about to start a new project and I think this time django is the way to go. I've been reading the documentation for the past two weeks and it looks promissing.
Ok, the thing is that I could not find anything about (in C# MVC called) Partial Rendering. For example if I want a dynamic menu where the menu-items comes from the database, then I would expect that the base template (or master page) renders the menu on each request (the partial renderer invokes another action or renders a template with some session data). So, the menu comes for free as long as my template inherits from this base template.
Honestly, I have no clue on how to achieve this.
What I would like is some code in the base template that uses data that is not contained in the child template. I don't want to include an extra variable (maybe 'menu_list_items') every time I call render_to_response('child_content.html',context). Is this possible?
Thanks!
You could either use a context processor, or a custom template tag to provide this functionality.
A context_processor is a simple function which can add objects to every RequestContext. A custom template tag can have its own template snippet and context which could render the menu for you.
For the template reusing: you should just create a base template for the generic layout, and use detailed templates for the individual pages. This is already covered in detail by the Django documentation.
What I tend to do for those generic parts (say for example, a menu highlighting the current part of the site the use is on), is to create my own render_to_response functions, akin to the following:
from django.shortcuts import render_to_response as django_render_to_response
def render_to_response(template, params, context_instance):
return django_render_to_response(template,
AppendStandardParams(request, params),
context_instance)
The ApplyStandardParams method then configures the menu based on the current path:
def AppendStandardParams(request, params):
if request.META['PATH_INFO'].startswith('/customer'):
params['category'] = 'customer'
params['title'] = 'Customer overview'
# and so on for all different parts
These category and title tags in this example are some values used to highlight the menu, configure titles, and so on. For example:
<!-- Customer menu entry: change class if this is the current category. -->
<li{% if category == "customer" %} class="selected"{% endif %}>Customers</li>
Finally, to use it in a view, instead of the normal render_to_response import, I just do something like from lib.views import *, which makes my custom version available in the view. This way the syntax of all code in the views stays the same, but I don't have to customize the menu every time I create a new view or app.

Django Flowplayer overlay does not submit form

I am trying to use flowplayer's overlay to load an external page that has a django form built in.
However the overlay loads the page but the submit button simply refreshes the page.
How do i actually submit the values entered in the form?
<script src="http://cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js"></script>
<script>
$(function() {
// if the function argument is given to overlay,
// it is assumed to be the onBeforeLoad event listener
$("a[rel]").overlay({
mask: {
color: '#ebecff',
loadSpeed: 200,
opacity: 0.9
},
effect: 'apple',
closeOnClick: false,
onBeforeLoad: function() {
// grab wrapper element inside content
var wrap = this.getOverlay().find(".contentWrap");
// load the page specified in the trigger
wrap.load(this.getTrigger().attr("href"));
}
});
});
</script>
<div class="bananas">launch</div>
my view boom has a model form.
Without seeing the actual view code, it's hard to give a helpful answer. In the future, please be sure to do so...
If you don't have the overlay programmed to redirect to the page, then submitting it to that same url might process/save the data without you noticing. Is the data being saved, or does absolutely nothing happen when you click 'submit'?
Generally, this is how it works: you need to be posting to a url, defined in urls.py, that points to a view function in your views.py. (These names are merely convention, and can be called whatever you like) You mentioned that you have a view named 'boom': is it defined in your urls.py like this?
url(r'^path/to/boom/$', 'model.views.boom',),
Check that this is defined and that your form is posting to it.
The view must then contain logic to process the request and return a response. Posting to that url will transfer a cleaned_data dictionary of form variables that can be accessed over the field names defined in the form. It looks like this: x = form.cleaned_data[x]. Check the form for its validity with form.is_valid(), and then do your processing. This can involve saving objects, running arbitrary code, whatever you wish.
To find out more, be sure to read the excellent documentation.

Avoid repeating the same query for object list in each Django view

I have a product list in my site's main menu.
My site's main menu is defined in my base template.
The product list is not hardcoded in template and is queried from DB.
So my base template requires product list to be in the request context of each view.
What is the best way to avoid querieng for product list and putting result into the request context in each view?
Thanks.
You can use template_context_processors. Create file, for example context_processor.py, and write in it method with argument request. Method must return values, in your case, it is product list. Then in settings file in TEMPLATE_CONTEXT_PROCESSORS option add your file_name.method_name. And in the base html use variable, which was returned.