1

Just started to learn python django framework ,i am trying to display a custom query result in html page.

def index(request):
    with connection.cursor() as cursor:
        cursor.execute("SELECT * FROM polls_post")
context ={
        'all_posts':cursor.fetchall()
    }

In my html page

<ul>
        {%  for post in all_posts %}
            <li>{{ post }}</li>
        {% endfor %}
    </ul>

But it displays the value as tuples ,so how can i get query result with table column name such as i can print the values like this way

<ul>
            {%  for post in all_posts %}
                <li>{{ post.title }}</li>
                <li>{{ post.name }}</li>
            {% endfor %}
        </ul>
2
  • 1
    If you just started to learn Django, what made you think the best way was to run a raw SQL query rather than use the well-documented model layer? Commented Nov 15, 2016 at 9:33
  • 1
    I need to know to this is possible thats all:) Commented Nov 15, 2016 at 9:35

3 Answers 3

3

You cannot - using cursor you need to repack data by yourself inside view and then pass the list of objects to the template.

Something like

posts = []

with connection.cursor() as cursor:
    cursor.execute("SELECT * FROM polls_post")

    for obj in cursor.fetchall():
        posts.append({"title": obj[0], "name": obj[1]})
context = {'all_posts':cursor.fetchall()}

However I suppose that you rather want to use Django's ORM framework. Take a look at the Model reference then.

Sign up to request clarification or add additional context in comments.

2 Comments

Is any shorthandway to do this?? without manually using the column name
@BlessanKurien, check documentation reference – docs.djangoproject.com/el/1.10/topics/db/sql There is example with use of cursor.description
1

Why are you use sql query in your view. Try to use Django ORM and QuerySets.

May be this is helps you

Django View

def your_view(request):
    all_posts = your_model_class_name.objects.all()
    return render_to_response("your_html.html", {'all_posts': all_posts})

And in you html page

<ul>
    {%  for post in all_posts %}
        <li>{{ post.title }}</li>
        <li>{{ post.name }}</li>
    {% endfor %}
</ul>

3 Comments

I will get unresolved reference error in polls_post
Pole post is your class name please replace it.
So i need to use ORM layer but i don't need it
1

You can also do like this

def your_view(request):
    all_posts = model_class_name.objects.raw("Select * from tablename");
    return render_to_response("your_html.html", {'all_posts': all_posts})

and in template

<ul>
    {%  for post in all_posts %}
        <li>{{ post.title }}</li>
        <li>{{ post.name }}</li>
    {% endfor %}
</ul>

raw query are helpful when we use complex queries

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.