I am trying to build a view in web2py that has multiple smartgrid objects served from the same controller. It displays them absolutely fine, but whenever I try to create a new record on the second table it doesn't allow entry, it just seems to refresh the page. Also trying to search on the second table actually fills in the search field on the first table too, so there is obviously some confusion as to which smartgrid is which.
In my research I came across the multiple form technique using process to name each form, see below:
form1.process(formname='form1')
However, this methodology doesn't seem to work for smartgrid objects (as far as I can tell). I guess I could try to create my own new SQLFORM.grid, but it seems a shame that I can't make better use of the smartgrids, as they have everything I need already.
Any help appreciated.
As you have noted, you cannot have two grids on the same page in this manner, as the grid makes use of the URL to determine its current state. Instead of the iframe approach, though, you might consider putting each grid in an Ajax component. In the main view file:
{{=LOAD('default', 'grid1.load', ajax=True)}}
{{=LOAD('default', 'grid2.load', ajax=True)}}
Of course, you can also serve both grids from the same action by specifying a URL arg to differentiate them.
To allow the grid machinery to deal with generated URLs like
/app/default/grid1.load/view/record/1?_signature=88ce76119afc68bbb141fce098cbc2eaf39289e3
for a view of a single record,
you must identify grids uniquely.
So construct your grids with formname keyword. Example:
def manage_records():
q_record = (db.record.au==auth.user_id)
return dict(record = SQLFORM.grid(q_record, formname='records'))
def manage_reports():
q_report = (db.report.au==auth.user_id)
return dict(record = SQLFORM.grid(q_report, formname='reports'))
Just as Antony have pointed, you can use LOAD() functionality.
You can omit ajax=True, if you want forms will load with a main page.
In the view with two grids:
<h2>Reports</h2>
{{=LOAD('default', 'manage_reports.load')}}
<h2>Records</h2>
{{=LOAD('default', 'manage_records.load')}}
Related
I am extending the wagtail rich text editor, draftail. I want to give the user the ability to insert a $RANDOM entity.
The site has a global option to set the random interval, let's call these random_start and random_end. These values are stored in the database.
When the $RANDOM entity is used, I would like the visited page to display a random number between random_start and random_end.
My current attempt allows me to generate the random number but the generation happens only when the page is published, not accessed. All subsequent page visits show the same number.
Previously (before switching to wagtail) the Django code was simple.
Do the following in page view code:
get random_start
get random_end
generate random number between them
pass generated number to the template
My current 'incomplete' solution is based of http://docs.wagtail.io/en/v2.5.1/advanced_topics/customisation/extending_draftail.html#creating-new-entities.
By modifying stock_entity_decorator, I get my current code.
def stock_entity_decorator(props):
"""
Draft.js ContentState to database HTML.
Converts the STOCK entities into a span tag.
"""
return DOM.create_element('span', {
'data-stock': props['stock'],
}, str(random.randint(random_start, random_end)))
Both, random_start and random_end are values which can change in the database.
I know I can use JavaScript to calculate the number client side. But I am hoping a solution exists which avoids client side calculation as that would introduce other problems.
Update
I had simplified the use case a little bit. The random number is not completely random, it is based of a few parameters which should preferably remain secret. Doing the calculation client side would imply disclosing these values.
I thought about setting up a RESTful endpoint and using client-side JavaScript to get the values that way.
For this project I am not concerned with caching and can disable it for the required pages.
You don't need to extend Draftail. Override the get_context() method of your Page-based model:
YourPage(Page):
def get_context(self, request):
context = super().get_context(request)
context['random_number'] = your_random_number_function()
return context
Wagtail Docs
Introduction
In Django, when the data you want to display on a template is included in one object, It's f**** easy. To sum up the steps (that everyone knows actually):
You Write the right method to get your object in your model class
You Call this method in your view, passing the result to the template
You Iterate on the result in the template with a for loop, to display your objects in a table, for example.
Now, let's take a more complex situation
Let's say that the data you want to display is widely spread over different objects of different classes. You need to call many methods to get these data.
Once you call these different methods, you got different variables (unsimilar objects, integers, list of strings, etc.)
Nevertheless, you still want to pass everything to a template and display a pretty table in the end.
The problem is:
If you're passing all the raw objects containing the data you need to your template, it is completely unorganised and you can't iterate on variables in a clean way to get what you need to display your table.
The question is:
How (which structure) and where (models? views?) should I organize my complex data before passing it to a template?
My idea on this (which can be totally wrong):
For each view that need "spread data" to pass to a template, I could create a method (like viewXXX_organize_data()) in views.py, that would take the raws objects and would return a data structure with organized data that would help me to display a table by iterating on it.
About the data structure to choose, I compared lists with dictionaries
dictionaries have key so it's cleaner to call {{dict.a-key-name}} rather than {{ tabl.3}} in the template.
lists can be sorted, so when you need to sort by date the elements you want to display, dictionary is not helpful, arghh, stuck again!
What do you think about all that? Thanks for reading until there, and sharing on this!
With your question you are entering in a conceptual/architectural domain rather than in a "this particular view of the data in my project is hard to represent in the template layer of django". So I will try to give you the birds view (when flying and not on the ground) of the problem and you can decide for yourself.
From the first philosophy box in the django template language documentation it's clearly stated that templates should have as little program logic as possible. This indicates that the representation of the data used in the template should be simple and totally adapted to the template you are trying to build (this is my interpretation of it). This approach indicates that you should have a layer responsible for intermediating the representation of your data (models or other sources) and the data that your template needs to achieve the final representation you want you users to see.
This layer can simple stay in your view, in viewXXX_organize_data, or in some other form respecting to a more complex/elaborated architecture (see DCI or Hexagonal).
In your case I would start by doing something like viewXXX_organize_data() where I would use the most appropriate data structures for the template you are trying to build, while keeping some independence from the way you obtain your data (through models other services etc).
You can even think of not using you model objects directly in the template and creating template specific objects to represent a certain view of the data.
Hope this helps you make a decision. It's not a concrete answer but will help you for sure make a decision and then staying coherent all trough your app.
I'm working on a web application on Google App Engine with Python as my backend language. Now, I need to calculate a particular value based on the user input and display it on my web page. Here's the concerned code sample:
<h4>Total Amount:{{disp.actual_price}}*{{quantity}}</h4>
Now I'm using Jinja 2 template for rendering my HTML pages. In the above example, 'disp.actual_price' is the value of the 'actual_price' attribute in my 'disp' entity of my Google App Engine datastore, while 'quantity' is a value passed on by the user. So basically, I'm unable to figure out a way to multiply these two variable values and displaying them on the webpage. If the actual_price is 300, and the value of 'quantity' is 2, then here's what gets displayed with my above code:
Total Amount:300*2
You should write it this way:
<h4>Total Amount:{{disp.actual_price * quantity}}</h4>
The correct answer to the question is don't do it. I'll explain why in a moment.
The correct syntax to do what you are attempting to do is:
<h4>Total Amount:{{disp.actual_price*quantity}}</h4>
From the documentation
There are two kinds of delimiters. {% ... %} and {{ ... }}. The first one is used to execute statements such as for-loops or assign values, the latter prints the result of the expression to the template.
In this particular case, either delimter would work fine. But you'd really want to use the second one. Your code didn't work because anything outside of the delimiter is treated as plain HTML. It has to be inside the delimiter to be treated as an expression.
But why is this wrong? You are mixing business logic with interface. Doing this means that the day you want to change the design of the page, or the workflow, you'll be juggling around business variables in the page. If the calculation got a little more complex, then your template does too. But worst of all, if you move to a different templating engine, then your entire application is screwed because all your business logic is stuffed inside the template.
To do this correctly you need to have a file that just does the calculations by itself. In your file where you have the template being called, you should import this logic file and call on the methods inside it. Thus, the method inside the file responding to the HTTP requests, might look like this:
#initialize template variable
totalAmount = importedLogicModule.calculateTotalAmount(disp.actual_price, quantity)
return template.render(totalAmount = totalAmount)
and the template would now be
<h4>Total Amount:{{totalAmount}}</h4>
I am working on an internal database that a handful of people will be using to access. In short there will be a non-authenticated search page that will allow them to do partial searched on various fields.
I am trying to figure out the best way to display upwards of 1000 individual components. Ideally it would show a list of N components on a page. But since I do not know the size of the query until they press search I don't know how to split it up.
I am fearful this is a case of I don't what I don't know.
I think you want Django's pagination. That should allow you to query a database, and split the resulting objects across pages. It even has an example.
I’m working on a Python script that accesses number of websites. I don’t think writing a whole class for each website is a feasible option, so I am looking for a way to reduce the code and re-use code as much as I can and write it as efficiently as I can. Although the website share same or similar engine (e.g. Wordpress), they often differ with slight details like cookie names, changed URL paths (e.g. example.com/signin.php and example2.com/login.php), data names and values for POST requests, and so on.
In most cases I could use variables, but sometimes I need to find extra data from retrieved url in the middle of method to continue, so that means adding few lines of code that needs to be processed before proceeding. I found that possible solution for me could theoretically be to use superclasses. But I couldn’t find any information how to actually ‘step into’ a method to make changes. To better illustrate my point, take a look at this example
class Wordpress(object):
def login(self):
# Do some stuff here
def newentry(self):
# Code to check if I'm logged in code
# and do bunch of other stuff
#
# Retrieve a page that *** sometimes contain important information
# in which case must be processed to continue! ***
data = {'name' : title,
'message' : message,
'icon' : iconid}
# Post new entry here
As I already commented in the example above in some cases I need to make adjustement just inside a method itself, add snippet of code, or change value of the variable or value in the dictionary, etc.. How can I achieve that, if it's even possible? Maybe my perception of superclasses is way off and they aren't really for what I think they are.
Maybe there's other way I haven't thought of. Feel free to post your own ideas how you would solve this problem. I am looking forward to your replies. Thank you.
Why can't you just call some process function from the main method, process can do whatever you want it to do, and you can even override it in derived classes e.g.
class WordPress(object):
def newentry(self):
data = get_data()
data = process_data(data)
# do moe generi cthings
def process_data(self):
return data