View not working / how can I test it in the shell?

178 views
Skip to first unread message

Mario Osorio

unread,
Oct 11, 2013, 9:48:13 PM10/11/13
to django...@googlegroups.com
Hello all, working with mezzanine, I created in an app (newsroom) folder, a views.py http://dpaste.com/1413587/. This view is an addition to the blog app in site-packages. I then I put a section in my blog_post_list.html template file to (supposedly) show the data from that view http://dpaste.net/show/6cHStytzI5cqfICpFVAT/, but I cannot see this data. blog_post_list.html is my home. Will someone please point my mistake here? How can I test this view in the shell.TIA!

donarb

unread,
Oct 11, 2013, 10:11:54 PM10/11/13
to django...@googlegroups.com
On Friday, October 11, 2013 2:48:13 PM UTC-7, Mario Osorio wrote:
Hello all, working with mezzanine, I created in an app (newsroom) folder, a views.py http://dpaste.com/1413587/. This view is an addition to the blog app in site-packages. I then I put a section in my blog_post_list.html template file to (supposedly) show the data from that view http://dpaste.net/show/6cHStytzI5cqfICpFVAT/, but I cannot see this data. blog_post_list.html is my home. Will someone please point my mistake here? How can I test this view in the shell.TIA!

What is featured_post_list? You assign it in the context, but it doesn't appear anywhere (except as the name of the view method). Perhaps you meant to pass "blog_posts" instead?

Mario Osorio

unread,
Oct 11, 2013, 10:14:38 PM10/11/13
to django...@googlegroups.com
You are right, I fixed that after posting as someone else point it out to me, but this is not working out for me nonetheless

Kelvin Wong

unread,
Oct 11, 2013, 11:19:49 PM10/11/13
to django...@googlegroups.com
In the shell:

>>> from django.test import Client
>>> c = Client()
>>> r = c.get('/about')
>>> r.context


K

Mario R. Osorio

unread,
Oct 11, 2013, 11:36:46 PM10/11/13
to django...@googlegroups.com
Thanks a lot Kelvin. I had already tried that, But I get nothing that tells me whether there is some error or not. I am 100% positive there is data that meets the criteria of my view nevertheless I neither see the data nor see an erroe message. :/


Dtb/Gby
=======
Mario R. Osorio
"... Begin with the end in mind ..."
http://www.google.com/profiles/nimbiotics


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/jLHmh2-Ami0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ee87ee00-d4a2-4ab6-8311-8f61370f8c50%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Mario R. Osorio

unread,
Oct 11, 2013, 11:37:47 PM10/11/13
to django-users
BTW, here is the output I'm getting: http://dpaste.com/1413745/


Dtb/Gby
=======
Mario R. Osorio
"... Begin with the end in mind ..."
http://www.google.com/profiles/nimbiotics


On Fri, Oct 11, 2013 at 7:19 PM, Kelvin Wong <wong...@gmail.com> wrote:

--

donarb

unread,
Oct 11, 2013, 11:53:42 PM10/11/13
to django...@googlegroups.com
You mentioned you don't see the data, but do you see anything, or just a blank page? In your last post you mentioned a warning about ALLOWED_HOSTS not being set when starting the shell. Is DEBUG set to False? If so ALLOWED_HOSTS must be set for security, otherwise a 500 error may not display anything on screen.

I was going to suggest that the next thing I'd try is to use the shell, and import the BlogPost class and enter the first two lines of code from your view, you should get something so you can verify the results are correct.


 

Mario R. Osorio

unread,
Oct 12, 2013, 12:25:54 AM10/12/13
to django...@googlegroups.com
From the server; I am getting the page I need but the block where the data from the view in question is supposed to be comes out empty.

From the django.test.client's Client() response.status.code is 200.

I am indeed geting the ALLOWED_HOSTS warning, which I don't fully understand and don't know hoe to fix. I do have ALLOWED_HOSTS = ('localhost', '.local') and DEBUG = True as I read in Rod's excellent tutorial, but that did not help.

Now, I dod not see that or any warning whatsoever as I start the shell, its only when I start the server that it shows. I have gotten 500 errors though.

I reported not seeing anything cause well; I did not know exactly what to expect from the test tool, which I now understand a bit more. In the mean time; I'm still not getting any data from my view and I still don't know how to test it in the shell.

Call my whatever you want, but I've been working on this issue for almost 3 days now: reading the docs, googling, reading the forums, asking, googling, asking, reading more articles and I still don't know where I'm standing.

I still don' know if my issue has to do with a bad written view or a bad located view, or a bad template, or something else.

SInce I understand more about databases than I do python or django or mezzanine or web app programming for that matter; I'd like to start by checking whether or not y view is returning what it is meant to and then move to other possibilities.

BTW yes, I am 120% positive there is data in my db that meets the criteria I'm trying to put in my view: I only have 7 records and 3 of them do meet the criteria.

Kelvin Wong

unread,
Oct 13, 2013, 4:34:22 AM10/13/13
to django...@googlegroups.com
You need to understand what ALLOWED_HOSTS does if you want to ever deploy your app. Please read it.


Check your settings in the shell

>>> from django.conf import settings as s
>>> s.DEBUG
True
>>> s.ALLOWED_HOSTS
['.example.com', '192.168.0.2']

If your DEBUG is False and your ALLOWED_HOSTS is an empty list [] then you will not get far. You will get a "SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS)" message.

You can try putting something like this into the template (pretty much straight from the Django blog example), and it will tell you nicely when it has nothing matching your query or if your db is empty:

{% block featured_posts %}
  {% if featured_posts_list %}
    {% for blog_post in featured_posts_list %}
        <p>{{ blog_post.title }}</p>
        <br>
    {% endfor %}
  {% else %}
    <p>No posts found</p>
  {% endif %}
{% endblock %}

You might also want to ensure that your view is rendering the right template and that the template hierarchy is correct (ie. {% extends "my_templates/base.html" %} is at the top of your template and it is correct). If your parent template that you're extending doesn't have a block named 'featured_posts' then it won't display anywhere. You won't see the "No posts found" message nor will you see your list of blogs - nada.

If there is an exception in the shell, it will print out a trace. This is an actual Dj1.5 trace from a made up exception I planted in the view. You can also put objects like your queryset into the Exception message and see if your set is producing what you think it should be.

>>> from django.test import Client
>>> c = Client()
>>> c.get('/')

Internal Server Error: /
Traceback (most recent call last):
  File "/Users/kelvin/.virtualenvs/myproject/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/kelvin/.virtualenvs/myproject/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/kelvin/.virtualenvs/myproject/lib/python2.7/site-packages/django/views/generic/base.py", line 86, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/kelvin/repos/myproject_files/myproject_repo/myproject/firstweb/views.py", line 14, in get
    raise Exception("Freek out!")
Exception: Freek out!



On Friday, October 11, 2013 5:25:54 PM UTC-7, Mario Osorio wrote:
From the server; I am getting the page I need but the block where the data from the view in question is supposed to be comes out empty.

Mario R. Osorio

unread,
Oct 13, 2013, 3:12:32 PM10/13/13
to django-users
Thanks Kelvin


Dtb/Gby
=======
Mario R. Osorio
"... Begin with the end in mind ..."
http://www.google.com/profiles/nimbiotics


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/jLHmh2-Ami0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages