TemplateDoesNotExist at / contactos /

时间:2016-11-02 15:51:36

标签: django django-views

我的观点出现问题:我在浏览器控制台的/contactos/处收到错误TemplateDoesNotExist

我正在使用django 1.10,我正在接受django权威指南的教程:正确的网络开发1.8。

我的观点:

from django.core.mail import send_mail 
from django.http import HttpResponseRedirect 
from django.shortcuts import render 

def contactos(request): 
errors = [] 
 if request.method == 'POST': 
    if not request.POST.get('asunto', ''): 
        errors.append('Por favor introduce el asunto.') 
    if not request.POST.get('mensaje', ''): 
        errors.append('Por favor introduce un mensaje.') 
    if request.POST.get('email') and '@' not in request.POST['email']:    
        errors.append('Por favor introduce una direccion de e­mail valida.') 
    if not errors: 
        send_mail( request.POST['asunto'],    request.POST['mensaje'],      
            request.POST.get('email', 'noreply@example.com'), 
            ['siteowner@example.com'],  ) 

        return HttpResponseRedirect('/contactos/gracias/') 
return render(request, 'formulario­contactos.html', {'errors': errors}) 

我的网址:

from django.conf.urls import url

from . import views

app_name = 'contactos'

urlpatterns = [

      url(r'^$',views.contactos, name ='contactos'),


]

模板路线/home/victor/desarrolloweb/ipsfa/contactos/templates/formulariocontactos.html

formulariocontacos.html

<!DOCTYPE html>
<html> 
<head> 
 <title>Contactanos</title> 
 </head> 
 <body> 
 <h1>Contactanos</h1> 
 {% if errors %} 
 <ul>  
 {% for error in errors %} 
    <li>{{ error }}</li> 
 {% endfor %} 
 </ul> 
 {% endif %} 

 <form action="/contactos/" method="post">{% csrf_token %}
 <p>Asunto: <input type="text" name="asunto" value="{{ asunto }}"></p> 
 <p>E­mail (opcional): <input type="text" name="email" value="{{ email     }}"></p> 
 <p>Mensaje: <textarea name="mensaje" rows="10" cols="50"  
 value="{{ mensaje }}"></textarea></p>  <input type="submit"    value="Enviar"> 
 </form> 
 </body> 
 </html>

回溯:

Traceback:

File "/home/victor/desarrolloweb/lib/python3.4/site-packages/Django-1.10.2-py3.4.egg/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/home/victor/desarrolloweb/lib/python3.4/site-packages/Django-1.10.2-py3.4.egg/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/victor/desarrolloweb/lib/python3.4/site-packages/Django-1.10.2-py3.4.egg/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/victor/desarrolloweb/ipsfa/contactos/views.py" in contactos
  20.     return render(request, 'formulario­contactos.html', {'errors': errors}) 

File "/home/victor/desarrolloweb/lib/python3.4/site-packages/Django-1.10.2-py3.4.egg/django/shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "/home/victor/desarrolloweb/lib/python3.4/site-packages/Django-1.10.2-py3.4.egg/django/template/loader.py" in render_to_string
  67.         template = get_template(template_name, using=using)

File "/home/victor/desarrolloweb/lib/python3.4/site-packages/Django-1.10.2-py3.4.egg/django/template/loader.py" in get_template
  25.     raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /contactos/
Exception Value: formulario­contactos.html

工作views.py

from django.shortcuts import render 

def contactos(request):  
    return render(request, 'formulariocontactos.html',) 

template working

0 个答案:

没有答案