0

I'm new with Django and i'm trying to show some data from my db but with no luck. The DB connection is OK and i did syncdb.

I'm trying to iterate on one column of my database (logging) table (handleName)

Model:

class Handle(models.Model):
    handleName = models.CharField(db_column='handleName', max_length=200)

    def __unicode__(self):  # Python 3: def __str__(self):
        return self.handleName 

View:

def logger(request):
    #query_results = Handle.objects.all()
    #return render(request, 'ate_logger/logger.html')

    query_results = Handle.objects.all()
    t = loader.get_template('ate_logger/logger.html')
    c = Context({
        'query_results': query_results,
    })
    return HttpResponse(t.render(c))

Html template:

    {% if query_results %}
        <ul>
        {% for handle in query_results %}
            <li> name {{ handle.handleName }} </li>
        {% endfor %}
        </ul>
    {% else %}
        <p>No data available</p>
    {% endif %}

The problem is that i'm getting No data available but i know for sure i have the data in the db. I've tried to use the tutorial but every thing i do seems OK so maybe someone can suggest a solution?

EDIT-1

I'll try to add some mote information i have a data base with data made by another software, i want to connect with Django to that database and show it's content. as first step i'm trying to iterate on table Handle column idHandle and to show all the values. the problem is that i have no return values in return self.handleName I have tried it also in the manage.py shell and it's also empty.

Kobi K
  • 7,743
  • 6
  • 42
  • 86
  • by `No data available`, do u mean that you get such an error or you do not have anything displayed? – Aswin Murugesh Oct 06 '13 at 07:39
  • @Aswin Murugesh i'm getting the string "No data available" from the {% else %} of {% if query_results %} meaning there is noting to iterate on – Kobi K Oct 06 '13 at 07:43
  • Verify again the variable name passed in context dict matches with variable in template. – Rohan Oct 06 '13 at 07:48
  • 1
    I recreated this and everything is working fine – yuvi Oct 06 '13 at 07:54
  • @yuvi i fixed return self.choice_text to return self.handleName but it didn't helped – Kobi K Oct 06 '13 at 07:55
  • @Rohan verified and it's the same – Kobi K Oct 06 '13 at 07:56
  • 1
    @KobiK do a `manage.py shell` then do `from mysite.models import *` and then do `Handle.objects.all()` and see if you really have any data or not – yuvi Oct 06 '13 at 08:01
  • @Rohan it's working for me too the problem is the the return is empty and i have data in the db.... i can't see the data. – Kobi K Oct 06 '13 at 08:04
  • Hi, please see Edit-1 maybe it will help to clear the problem. – Kobi K Oct 06 '13 at 08:14
  • 1
    Did you include your app in your installed_apps? This seems like a problem with connecting to the db, either your model is wrong and doesn't fit the table, you DB setup in settings.py is wrong or something else. It's hard to say exactly when you ommit part of the fields and don't show us how the db looks like. I can only tell you that all the code you are showing here is absolutely fine – yuvi Oct 06 '13 at 08:39
  • @yuvi thank you for your help, i think i have a general problem, in my model i have to model my entire table or i can just model part of it? in my example i have included only one col of the db table which in fact it has 8 cols. – Kobi K Oct 06 '13 at 08:45
  • 1
    The model obviously needs to fit the table, and you probably also need to tell it specifically what table in your db to use. Research a little about connecting a django app to an existing db and come back to SO if you're facing trouble. Here's a useful link to get you started: https://docs.djangoproject.com/en/dev/howto/legacy-databases/ – yuvi Oct 06 '13 at 08:51
  • That was most helpful!! one last thing shoe can i tell it which table in your db to use? i thought it's by the class name in the model... – Kobi K Oct 06 '13 at 08:55

1 Answers1

0

Problem is solved by @yuvi help!

Auto modeling the db was the needed option.

docs.djangoproject.com/en/dev/howto/legacy-databases

Kobi K
  • 7,743
  • 6
  • 42
  • 86