Django cannot find my templates directory

97 views
Skip to first unread message

Cherrymae Tulfo

unread,
Mar 10, 2014, 10:25:42 PM3/10/14
to django...@googlegroups.com
hi everyone!

i am looking for solutions with regards to templates directory . . i have this results.html where the data should be displayed after it is queried through views.py. . it should display the barangay id and the name of the barangay itself. .

{% block content %}
{% if results %}
  {% for result in results %}
    {{ result.id }}
    {{ result.barangay }}
  {% endfor %}
{% else %}
    <h3 class='error'>Please enter a valid UID</h3>
    <form method="get" action="/search/">
      Search Barangay: <input type="text" name="q" id="id_q" value="{{ query     }}"/>
      <input type="submit" value="Search" />
    </form>
{% endif %}

{% endblock %}

i have also views.py that queries data from the postgresql

from django.template import RequestContext
def search(request):

    query = request.GET.get('q')
    try:
        query = int(query)
    except ValueError:
        query = None
        results = None
    if query:

        results = Butuan_Parcel.objects.get(id=query)

    context = RequestContext(request)

    return render('results.html', {"results": results}, context_instance=context)

i want also to know what am i missing with this. .urls.py

(r'search', search)


when I search id in my index.html , the system can retrieve the data but it gives me an error 
TemplateDoesNotExist at /search/. . what am I missing??
it was also suggested to to declare my template directories
so i declared this in my settings.py


PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_DIRS = (PROJECT_PATH+'/Apps/templates'
)


yet, nothing has worked . .i still get the error. . can anyone suggest solutions with my problem?? Thanks in advance. .

Mike Dewhirst

unread,
Mar 11, 2014, 1:27:01 AM3/11/14
to django...@googlegroups.com
On 11/03/2014 1:25pm, Cherrymae Tulfo wrote:
> hi everyone!
>
> i am looking for solutions with regards to templates directory . . i
> have this results.html where the data should be displayed after it is
> queried through views.py. . it should display the barangay id and the
> name of the barangay itself. .
>
> |*{% block content %}
> {% if results %}
> {% for result in results %}
> {{ result.id }}
> {{ result.barangay }}
> {% endfor %}
> {% else %}
> <h3 class='error'>Please enter a valid UID</h3>
> <form method="get" action="/search/">
> Search Barangay: <input type="text" name="q" id="id_q" value="{{ query }}"/>
> <input type="submit" value="Search" />
> </form>
> {% endif %}
>
> {% endblock %}
> *
> |
>
> i have also views.py that queries data from the postgresql
>
> *|from django.template import RequestContext
> def search(request):
>
> query = request.GET.get('q')
> try:
> query = int(query)
> except ValueError:
> query = None
> results = None
> if query:
>
> results = Butuan_Parcel.objects.get(id=query)
>
> context = RequestContext(request)
>
> return render('results.html', {"results": results}, context_instance=context)|*
>
> *||
> *i want also to know what am i missing with this. .urls.py*
> *
>
> |*(r'search', search)*

Might need to see a bit more of your urls.py file.

>
>
> |
>
> ||when I search id in my index.html , the system can retrieve the data but it gives me an error
> TemplateDoesNotExist at /search/. . what am I missing??

Django is looking in a default location for results.html. You need to
make sure it is found or more particularly it is finding the correct
results.html template. Check the following ...

In your settings.py module ...

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)

# if templates are not found here, look in BASE_DIR/app_name/templates
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates/'),
# any other non-default dirs here
)

Also in your settings.py insert a print function to display your
template dirs perhaps like this ...

if DEBUG:
for i in enumerate(TEMPLATE_DIRS, 1):
print(('TEMPLATE_DIRS#%s = %s' % i))


Note that with a specific template directory set as above,
filesystem.Loader will look in there first before looking in the
app_name/templates directories. I use that specific directory as a place
for common templates which get inherited by all the application
templates. I use it for the html error code templates which need to
carry my error messages etc.

Where you see "# any other non-default dirs here" in my TEMPLATE_DIRS
setting, I could have additional common template dirs if I needed to
keep them separate for some reason.


> it was also suggested to to declare my template directories
> so i declared this in my settings.py
>
> *
> PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
> TEMPLATE_DIRS = (PROJECT_PATH+'/Apps/templates'*)

Provided you have the template loaders in the correct sequence it should
work.

Although I'm not sure what those asterisks might be doing.

>
> yet, nothing has worked . .i still get the error. . can anyone suggest
> solutions with my problem?? Thanks in advance. .
>
> ||
>
> ||
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/6a0bc697-05bc-4952-9cd1-705422b9bbba%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/6a0bc697-05bc-4952-9cd1-705422b9bbba%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages