2015-10-20 45 views
-1

那么,我设计了一些东西,但我不知道,如何实现 它。包含块没有显示在Django模板

models.py

class Notificaciones(models.Model): 
IDcliente = models.ManyToManyField(User) 
Tipo_de_notificaciones = ((1,'Ofertas'),(2,'Error'),(3,'Informacion')) 
Tipo = models.IntegerField('Tipo de notificacion',choices=Tipo_de_notificaciones, default=3,) 
Nombre_not = models.CharField("Nombre de la notifiacion",max_length=50) 
Descripcion_not = HTMLField("Descripcion de la notificacion") 
Imagen_not = models.ImageField("Imagen de la notificacion",upload_to="notificaciones") 
Fecha_Caducidad_notificacion = models.DateTimeField("Fecha de caducidad de la notificacion",auto_now_add=False) 
class Meta: 
    verbose_name = 'Notificacion' 
    verbose_name_plural = 'Notificaciones' 
def __str__(self): 
    return self.Nombre_not 

views.py

def notifi(request): 
notifi = Notificaciones.objects.all() 
return render_to_response('app/notificaciones.html',{ 'notifi' : notifi }) 

现在我要显示在页眉通知在灯箱广告,然后在我的layout.html,其中标题,页脚等被调用。但是当我打电话通知时,它不会出现。

<div id="notifiaciones" class="notificaciones notificacionesTrans" tabindex="-1" role="dialog" aria-hidden="true" > 
    {% include 'app/notificaciones.html' %} 
</div> 

有人可以解释,如果我可以打电话从意见通知还是应该以某种方式做别的?

URL.PY

url(r'^tinymce/', include('tinymce.urls')), 
url('', include('django.contrib.auth.urls', namespace='auth')), 
url(r'^social/',include('social.apps.django_app.urls', namespace='social')), 
#url(r'^s$', 'app.views.CategoriaProductoss', name='servicios'), 
#url(r'^s/(?P<id>\d+)$', 'app.views.servicioscategoria', name='servicioscategoria'), 
url(r'^notificaciones/$', 'app.views.notifi', name='notificaciones'), 
url(r'^media/(?P<path>.*)$','django.views.static.serve', {'document_root':settings.MEDIA_ROOT,}), 
url(r'^$', 'django.contrib.auth.views.login',{'template_name':'app/index.html'}, name='Vulpini.co'), 
url(r'^$', 'django.contrib.auth.views.logout', name='logout'), 
url(r'start$', 'app.views.start', name="start"), 
url(r'ajax-upload$', 'app.views.import_uploader', name="my_ajax_upload"), 

# Uncomment the admin/doc line below to enable admin documentation: 
url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 

# Uncomment the next line to enable the admin: 
url(r'^admin/', include(admin.site.urls)), 

Notificación.html

<ul> 
{% for notifi in notifi %} 
    <li>{{ notifi.Tipo }} 
     {{ notifi.Nombre_not }} 
     <img src="{{ notifi.Imagen_not }}" alt="{{ notifi.Nombre_not }}"/> 
     {{ notifi.Fecha_Caducidad_notificacion }} 
    </li> 
{% endfor %} 
</ul> 

登录表单内的layout.html

<form action="/login" class="form-horizontal" method="post"> 
           {% csrf_token %} 
           <h4>Iniciar Sesion.</h4> 
           <hr /> 
           <div class="login-social">      
             <a href="{% url 'social:begin' 'facebook' %}?next={{ request.path }}" target="iframe">Iniciar sesion con Facebook</a>   
             <a href="{% url 'social:begin' 'twitter' %}?next={{ request.path }}" target="iframe">Iniciar sesion con Twitter</a> 

           </div> 
           <hr /> 
           <div class="form-group"> 
            <label class="control-label" for="inputEmail">Usuario</label> 
            <div class="controls"> 
             <input name="username" type="text" id="inputEmail" placeholder="Usuario"/> 
            </div> 
           </div> 
           <div class="form-group"> 
            <label class="control-label" for="inputPassword">Contraseña</label> 
            <div class="controls"> 
             <input name="password" type="password" id="inputPassword" placeholder="Contraseña"/> 
            </div> 
           </div> 
           <div class="form-group"> 
            <label class="checkbox"> 
            <input type="checkbox" />Recordar</label> 
            <button type="submit" class="btn btn-info">Ingresar</button> 
            <a href="/">Registrar</a> 
           </div> 
          </form> 
+0

在这里一切似乎都没问题。显示你的'app/notificaciones.html'。那么'urls'呢? 'inclide'模块只能插入html,而不是来自视图的数据,所以你应该自己将它绑定到所需的模板。 – chem1st

+0

@ Alfredhb.q你可以编辑你的文章向我们展示app/notificaciones.html和你的URLs.py文件吗? – user2719875

+0

@ user2719875完成后,我编辑了帖子,并显示我的notificaciones.html和URLs.py –

回答

0

的问题是,django.contrib.auth.views.login就是呈现index.html页面(你可以从这里看到):

url(r'^$', 'django.contrib.auth.views.login',{'template_name':'app/index.html'}, name='Vulpini.co'), 

index.html页面扩展的layout.html和的layout.html包括notificaciones.html。这些模板在任何时候都不会通过'notifi'变量(这就是为什么什么都没有出现 - 因为django.contrib.auth.views.login没有向您的模板传递任何'notifi'变量)。一旦做到这一点,那么指数

def index(request): 
notifi = Notificaciones.objects.all() 
return render_to_response('app/index.html',{ 'notifi' : notifi }) 

:为了完成你想做的事,网址改成这样:

url(r'^$', 'app.views.index', name='Vulpini.co'), 

,然后在views.py,添加了这种观点。 html(它扩展了layout.html,它调用notificaciones.html)将可以访问'notifi'变量。然后在你的index.html模板,你可以做一个表单提交到“/登录”,它采用django.contrib.auth.view.login,像这样:

url(r'^login$', 'django.contrib.auth.views.login', name='Vulpini.co'), 

,并在你的settings.py,设置此:

LOGIN_REDIRECT_URL = '/' 

在登录后回index.html页面重定向

编辑:由于这是核对答案,我想指出的是另一种选择(如chem1st在说他的回答),将在这里看一下上下文处理器:https://docs.djangoproject.com/en/1.7/ref/templates/api/#writing-your-own-context-processors

查看chem1st的答案获取更多信息。

+0

好的,这个工作,然后现在我必须做一个表格?,并在布局后调用它? –

+0

@ Alfredhb.q如果你想登录一个用户,然后在layout.html(或任何你想要的)中创建一个表单,该表单对“/ login”URL(例如

)进行了处理。然后表单将数据发布到调用django.contrib.auth.views.login方法的“/ login”(当用户提交表单时)。然后,django.contrib.auth.views.login将处理登录,并将重定向回'/'(如settings.py中的LOGIN_REDIRECT_URL所述)。 – user2719875

+0

我要再次编辑主要问题,您可以看到我的表单看起来如何。 –

0

包含和扩展块不会将数据从视图传递到模板。如果你想从视图中获取,请明确传递它。

您还应该查看context processors,因为它们将允许您全局实现所需的数据。