Re: Difficulties using generic views (Tutorial 4, Django 1.4)

196 views
Skip to first unread message

Sergiy Khohlov

unread,
Jul 19, 2012, 2:54:37 AM7/19/12
to django...@googlegroups.com
Please provide path to your templates

2012/7/19 Ricardo Cardoso <morg...@gmail.com>:
> I'm new to django and python, and decided to follow up the available
> tutorials to get up to speed. These were helpful and got me on my feet but
> while finishing up the tutorial i hit a major snag. While everything was
> working properly using custom views, i can't seem to make my app load the
> correct templates after switching to generic views.
>
> Here's the code:
>
> #urls.py
> from django.conf.urls import patterns, include, url
> from django.views.generic import DetailView, ListView
> from polls.models import Poll
>
> urlpatterns = patterns('',
> url(r'^$',
> ListView.as_view(
> queryset=Poll.objects.order_by('-pub_date')[:5],
> context_object_name='latest_poll_list',
> template_name='polls/index.html')),
> url(r'^(?P<pk>\d+)/$',
> DetailView.as_view(
> model=Poll,
> template_name='polls/detail.html')),
> url(r'^(?P<pk>\d+)/results/$',
> DetailView.as_view(
> model=Poll,
> template_name='polls/results.html'),
> name='poll_results'),
> url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
> )
>
> The way i decided (perhaps wrongly) to organize my apps and templates is as
> follows:
>
> Template dir as defined in settings.py:
> TEMPLATE_DIRS = ('/home/my_user/Webdev/Templates/', # this is where i'm
> storing my templates at the moment
>
> App location: /home/my_user/Webdev/Django-lessons/pollproject/polls
>
> If i try to access the polls app directly by using the URL:
> http://127.0.0.1:8000/polls/ i now get an error message stating that:
>
> TemplateDoesNotExist at /polls/
>
> index.html, polls/poll_list.html
>
>
>
> I realize this might have something to do with where i decided to store my
> templates (made more sense at the time to store them separately since i
> thought i could use different templates for the same app, for different
> purposes).
>
> This is probably a basic issue but any help regarding this would be greatly
> appreciated.
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/gWDhmGFeSlEJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Carlos Palol

unread,
Jul 19, 2012, 5:26:19 PM7/19/12
to django...@googlegroups.com
    url(r'^$',
        ListView.as_view(
            queryset=Poll.objects.order_by('-pub_date')[:5],
            context_object_name='latest_poll_list',
            template_name='polls/index.html')),


Here you are setting template_name='polls/index.html'
 

TemplateDoesNotExist at /polls/

index.html, polls/poll_list.html


But this error corresponds to having set template_name='index.html' in this same view.

Review you really have set  template_name='polls/index.html' in that view and that this file is in place:
/home/my_user/Webdev/Templates/polls/index.html

 Cheers

Carlos Palol

unread,
Jul 23, 2012, 7:54:22 AM7/23/12
to django...@googlegroups.com
Let's see the whole error page.


On Sunday, 22 July 2012 23:43:59 UTC+2, Ricardo Cardoso wrote:
Thank you for your input guys. 

Sadly, i had already checked it Carlos and have my templates for polls under:
/home/my_user/Webdev/Templates/polls

In this case my template dir is set up as:
/home/my_user/Webdev/Templates/

Should i create another polls dir under templates (/home/my_user/Webdev/Templates/polls/polls) considering in the error message i get 'polls/poll_list.html' , besides 'index.html'? This seems redundant and i'd like to understand why i would have to. 

Thanks again for your help.

On Thursday, July 19, 2012 12:08:39 AM UTC+1, Ricardo Cardoso wrote:
I'm new to django and python, and decided to follow up the available tutorials to get up to speed. These were helpful and got me on my feet but while finishing up the tutorial i hit a major snag. While everything was working properly using custom views, i can't seem to make my app load the correct templates after switching to generic views.

Here's the code:

#urls.py
from django.conf.urls import patterns, include, url
from django.views.generic import DetailView, ListView
from polls.models import Poll

urlpatterns = patterns('',
    url(r'^$',
        ListView.as_view(
            queryset=Poll.objects.order_by('-pub_date')[:5],
            context_object_name='latest_poll_list',
            template_name='polls/index.html')),
    url(r'^(?P<pk>\d+)/$',
        DetailView.as_view(
            model=Poll,
            template_name='polls/detail.html')),
    url(r'^(?P<pk>\d+)/results/$',
        DetailView.as_view(
            model=Poll,
            template_name='polls/results.html'),
        name='poll_results'),
    url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)

The way i decided (perhaps wrongly) to organize my apps and templates is as follows:

Template dir as defined in settings.py:
TEMPLATE_DIRS = ('/home/my_user/Webdev/Templates/', # this is where i'm storing my templates at the moment

App location: /home/my_user/Webdev/Django-lessons/pollproject/polls

If i try to access the polls app directly by using the URL: http://127.0.0.1:8000/polls/ i now get an error message stating that:

TemplateDoesNotExist at /polls/

index.html, polls/poll_list.html


Ricardo Cardoso

unread,
Jul 23, 2012, 10:11:01 AM7/23/12
to django...@googlegroups.com
Oddly, when i started up the server this morning i could now see the polls correctly, while not having changed my code, by accessing http://127.0.0.1:8000/polls. Don't know if Django is prone to such randomness but am happy that it works now. Tested everything, added a view so that http://127.0.0.1:8000 loads polls, voted and viewed results. All fine.

I'll carry on testing this and getting a couple more tutorials done before moving to my first django production.

Thank you for your time and patience Carlos, much appreciated.




Carlos Palol

unread,
Jul 23, 2012, 5:05:16 PM7/23/12
to django...@googlegroups.com
I'm glad.

Not very prone to randomness, actually. Make sure the web server is restarting after your changes, and that the browser cache is not lying to you.

Also, if you are changing module (file) names, better delete the .pyc files.

Cheers,

----
Carlos Palol
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/CrRjT-ql1AMJ.

Rob

unread,
Jul 24, 2012, 7:58:06 AM7/24/12
to django...@googlegroups.com
Sometimes the browser cache is making trouble too. Especially with Firefox I occasionally have this kind of problems. Clear the browser cache, or test with a different browser mostly helps.
Reply all
Reply to author
Forward
0 new messages