Django Admin passing data to change_list_result.html - django

The template change_list_results.html recieves a parameter which is called results. It contains every record (in db) in a raw_html for printing it in change_list.html.
I want to change the view which controls this results, for returning into it every value of the recodĀ“s tables (as object) instead of a raw_html within it ( kind of ).
Someone knows where is this parameter sending to the template?
Thanks mates.

Found it. If someone want to know you must to override the inclusion tag called result_list over change_list_result.html. Cheers

Related

Passing cfquery to a cfc function from an element bind attribute

I have a cfc function that accepts a query type argument (with the intent of running a query of query).
I am able to call the function successfully using <cfinvoke>.
However, what I really want to do is call the function from a hidden <cfinput>'s bind attribute, using
bind="cfc:cfcname.somefunction(queryVar)", and that code fails. There is no visible error, but it doesn't look like my function ever gets called - I have a cflog in there.
[The reason for the bind is not really relevant to my question, but it is because I need the function call to react to another control on the form - I've distilled all that out of my question]
If I replace the queryVar with some string variable, the function gets called fine, but obviously I cannot get the results I want without passing the query in.
If I use bind="cfc:cfcname.somefunction(#queryVar#)", I get a
"Complex type cannot be converted to simple type"
error.
I've searched for any documented restriction on passing query (or any complex type) to a cfc from a bind, but haven't found a clue.
You can use "bind" to send user-inputted data to another page for processing then send back the results. For example, If you use "bind" on a Birthdate field. When the user selects their birthdate, you can send that date to a CFC or CFM page and have it do the math #DateDiff('yyyy',birthdate,Now())# and return the result, which would be how many "years old" the user is.
But to my knowledge, you can't send a query to another page via the "bind" function. However, you could run the query again on another page and return the results.
Or perhaps look into JQuery to handle further processing after the page has loaded.

How to store a dynamic site-wide variable

I have an html file which is the base,where other html documents extends.Its a static page but i want to have variable in the menu.I don't think it's wise to create a view for it,since i don't intend to let users visit the base alone.So where in my project can I store site-wide dynamic variables that can be called on any page without explicitly stating them in their views.
Thank you in advance.
For user specific variables, use session.
For global constants (not variables!), use settings.py.
For global variables, consider to store it in database so it can be multithreading & multiprocess safe.
I looked around and saw different approaches,but one that doesn't compromise the DRY philosophy the most for me is registering a tag in your project then input it in the base template.Its neater See here https://stackoverflow.com/a/21062774/6629594 for an example
Storage can take any number of places, I put mine in a stats model in the db so you get all the goodness of that (and make it easy to access in views).
I then have a context processor written as so:
#context_processors.py:
def my_custom_context_processor(request):
return {'custom_context_variable1':'foo','custom_context_variable2':'bar'}
Add this to your context processors in settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"my_app.context_processors.ny_custom_context_processor",
)
Provided you use render() to render your templates you can then you can just use:
{{ custom_context_variable1 }}
to return 'foo' in your template. Obviously returning strings is for example only, you can use anything you like so long as your context processor returns a dict.
you can also try using php pages.
Then acces the variable on each page with an include 'file containing the var.php' on every page.
None of this will be visible in the source html as it is only processed on the server side.
If you you would like to try this, mail me and I will send you some sample code.

Django object_name rendering in template

I've seen a SO post on this but can't seem to find it.
My results are rendering in my template with the object_name as
{'sessions__sum': 29649}
instead of just
29649.
How can I show just the sum? Thank you.
Update
I realize, out of lack of knowledge, I may not have asked my question correctly, so below is my view and a screenshot of my template, rendered in the browser. Hopefully this will help clear things up.
view
year_weekly_filter.filter(created__week_day=1).aggregate(Sum('sessions'))
monday2014 = year_weekly_filter.filter(created__week_day=2).aggregate(Sum('sessions'))
Template
{{ monday2014 }}
ScreenShot - how its rendering in the browser
You should note that: "aggregate() is a terminal clause for a QuerySet that, when invoked, returns a dictionary of name-value pairs." - Django Docs
If you just want a single value to be returned to a variable and not a dict, use .get like so:
monday2014 = year_weekly_filter.filter(created__week_day=2).aggregate(Sum('sessions')).get('sessions__sum',0)
Do you mean the {{object}} in the template is rendered by the dictionary object?
In this case, the key is accessed in this way.
{{object.sessions__sum}}
and you may get help in this and the related documentation is here.
I don't have enough points to add a comment, so I leave my guess here.

How to check for a value's datatype inside django template?

I want to get the datatype of a value in django template. I am sure there is a way because when you do
value|date:"Y-m-d"
django template understands that the value is timestamp. How does django do it?
the "|date" you see is called a template filter and is a built-in function of django
you may want to create one of your own (here's how) that takes something as input and returns its datatype.
but, imo, it's not a best-practice, since a template should mostly be used to just display and format data. if you need an evaluation on the type i suggest you move that inside a view and return the result in the context, eventually

Django regroup tag problem

I have a custom filter user_tz which takes user as an argument. It works fine everywhere, but when I tried to use this filter in the regroup tag it fails saying that user does not exist.
The code:
{% regroup proj_messages.object_list by created_on|user_tz:user as proj_message_list %}
This is the error I am getting:
Caught an exception while rendering: Failed lookup for key [user] in u"Today's tasks".
Thanks,
Masood Ahmed
Strangely even I came across this exactissue.
Though later I had to change my code, i couldn't get on its solution. Apparently what happens is something like as follows.
Whatever you write in templates, is splitted as Nodes, Variables etc. Variables, at time of rendering is searched in context available to that instance. And please note that, for regroup tags, the context available, is just that object_list, which you passed as first argument. Not the usual context(which contains 'user' in your case), which is global to whole template. So, it is unable to find any other variable you specified, which is not in you object_list.
Thus, in your case, the context available to regroup is an object from proj_messages.object_list. And so the resolver code is able to find variable created_on in context, but not user. And that is what throwing the template exception here.
Considering this just imagine, what would happen, if your object_list too, have had the user attribute. In that case there wont be any KeyError, but user passed to the filter, would be not at all the user variable you intended to pass.
Edit on request:
There is not direct way, to pass the user to the such use of filter in regroup tag. But a kind of hack will obviously work. Note, its just a hack. Make each individual entity/object of proj_messages.object_list to contain that user variable from view or using an extra filter on object_list from template.
But better than that, if you want user to be available anywhere, from outside the context, i would like you to consider yet another hack. Check out, http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser .