ProgrammingError at /user_booking not all arguments converted during bytes formatting - mysql

The idea is to display user's booking information like booking_name, package etc. I am fetching the data from database in my views.py like this.
def user_booking(request):
id = request.session['u_id']
booking_data = Booking.objects.raw('select * from booking WHERE id = %s',id)
return render(request,'user_booking.html',{'view_data':booking_data})
Now in user_booking.html is displaying data. I have used a proper template to display data in tabular format. Problem is when some data is returned from data base it works perfect. When there is a query for which no data can be found the page breaks and shows
"ProgrammingError at /user_booking not all arguments converted during bytes formatting"
Instead of all the formatting I resorted to basic code which looks like this
<html>
{% for b in view_data %}
{{b.b_name}}
{% endfor %}
</html>
It gives me the name "ajay" as output and does not break the page, also note that record for ajay's ID is present in my database. Now if ID is changed and then I refresh the lage again it gives me error.
What I am trying to do is if there is any data coming from the database then display it otherwise just display "no data here" msg.
How do I achieve it?

Related

Comparing lists in DBT jinja macro inside for loop

I am storing output of each column in a list for the below query
select table,
columns,
flag
from {{ model }}
for each table I need to compare the flag. how do i get flag in list for the corresponding table
{% for n in table %}
{%- if flag[n] == 'False' %}
I tried as above its not working its returning null value
It's important to understand that jinja is a templating language. In dbt, we use jinja to automate the creation of sql that then gets executed in our data warehouse. Accordingly, data from your data warehouse does not (typically) flow through your jinja code.
flag is a reference to a column in your database, so to evaluate the contents of flag, you need to write sql to do that job.
The only exception is if you are using run_query or a similar macro to pull the data from your database into your jinja context at model-compile time. But I don't see any reference to that in your code (and frankly that's a complicating detail that's only relevant if you are trying to write SQL dynamically). If you are just trying to compare the value from a record in your database as part of your model, just do it in sql.

How do I return a value from database to template?

I have a database that uses formset to help store data. How do I get the ‘value’ of the numbers of data in the template? For example, I got a queryset of {[Item 1],[ Item 2]}. How do I get the value of 2 in the template to tell me there's 2 items? I want to use this value to control the amount of stuff I can clone with a click of the button. I'm using Django for the web
You just ask |length of the queryset, like:
{{ queryset|length }}
will be returning the number of element

How to display values from mysql database table into a html page using cherrypy?

how to to render values from database(mysql) table into my html page using cherrypy?
Actually what I am trying to do is, I have a html page and I want to display values from database in fields against each label.
I have searched and searched a lot, and what i found is this:
#cherrypy.expose
def extract(self):
cur = db.cursor()
cur.execute('select count(*) from config')
res = cur.fetchone()
db.commit()
cur.close()
return "<html><body>Hello, you have %d records in your table</body></html>" % res
Instead of creating a new page in the return statement i want these database values to display in my html page, corresponding to their labels.
How to do that in python using cherrypy?
test.html this is the link to my html page where in textboxes against the labels i want to display values from database table.
How to achieve this?
PS: I am a newbie to both python and cherrypy, any help would be appreciated.
One solution is using Template engines- jinja2 or mako as explained by #webKnjaZ in comment section.
Second solution is:
Return json data from pyhton function and make ajax call from html page to get the data. And once you have json data in your html , you can parse that data and display it in your html page.

Can I allow paging in the contenttype config

I have been playing with the paginator function in Bolt CMS, it is easy to use.
Now I need to know if there is a way to implement the pagination in the contenttype yaml.
I think, is it possible something like this?
entries:
name: Entries
singular_name: Entry
fields:
...
taxonomy: [ categories ]
allowpaging: true
I only have found that you need explicity write the allowpaging flag when you fetch the content via setcontent:
{% setcontent entries = "entries/latest/4" allowpaging %}
But what if you want to use the same template for displaying the related taxonomy records? The problem is that you always will be fetching the last 4 entries regardless the taxonomy.
If there's no way to do this, there would be a way to implementing it?
Paging will also be automatically set if you use listing records setting
listing_records: 10
But your template still needs a pager that will use this setting - the listing templates in the theme/base-2014 will work and can be used as an example
The docs have more information https://docs.bolt.cm/contenttypes-and-records#defining-contenttypes
In config.yml set listing_records: xx or the number of records you want to show
then set in your .twix template {% setcontent entries = "entries/latest/xx" allowpaging %}with the same number
and add at the end of .twix file put this code {{ pager('pages') }} to show pages
You can see the official bolt docs for further informations
https://docs.bolt.cm/3.1/templating/content-paging

Flask SQLAlchemy does not commit until App ends

I've started my session like so:
from flask.ext.sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://mydatabase'
db = SQLAlchemy(app)
And I'm Adding entries to my database like so:
db.session.add(user(name, password))
db.session.commit()
Immediately after I add it, I render a new template. When the template's rendered, it should include the data I've just added, but it doesn't. However, when I end my app with a ctrl-c, then restart it, the data I've added now shows.
The data is submitted using a GET. The arguments are obtained using flask.request.args.get and it gets the name and password. Then the data is added using the db.session.add thing above.
After the db.session.add is done, the data is obtained from the entire database like so:
users = []
for userentry in user.query.all():
users.append(userentry.name)
In the template:
{% for name in users %}
<p>{{name}}</p>
{% endfor %}
Then template is rendered using: return render_template('my_page.html', users=users)
It seems I've been using an older design pattern that I found on the Flask Mega Tutorial. I solved my problem by opening up individual sessions for adding data. http://docs.sqlalchemy.org/en/rel_0_7/orm/session.html