Javascript django模板中未显示包含块

Javascript django模板中未显示包含块,javascript,python,html,django,django-models,Javascript,Python,Html,Django,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_not

嗯,我设计了一些东西,但我不确定如何实现 它

型号.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
def notifi(request):
 notifi = Notificaciones.objects.all()
 return render_to_response('app/notificaciones.html',{ 'notifi' : notifi })
视图.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
def notifi(request):
 notifi = Notificaciones.objects.all()
 return render_to_response('app/notificaciones.html',{ 'notifi' : notifi })
现在,我想在lightbox的页眉中显示通知,然后在my layout.html中显示通知,其中调用页眉、页脚等。但当我调用通知时,它不会出现

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

{%include'app/notificaciones.html%}
有人能解释一下我是可以从视图调用通知,还是应该以其他方式调用通知

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)),
url(r'^tinymce/',include('tinymce.url'),
url(“”,包括('django.contrib.auth.url',namespace='auth')),
url(r“^social/”,包括('social.apps.django_app.url',namespace='social'),
#url(r“^s$”、“app.views.CategoriaProductoss”、name='servicios'),
#url(r'^s/(?P\d+)$,'app.views.servicoscategoria',name='servicoscategoria'),
url(r“^Notifications/$”,“app.views.notifi”,name='Notifications'),
url(r'^media/(?P.*)','django.views.static.service',{'document\u root':settings.media\u 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\u uploader',name=“my\u ajax\u upload”),
#取消注释下面的admin/doc行以启用管理员文档:
url(r“^admin/doc/”,包括('django.contrib.admindocs.url'),
#取消注释下一行以启用管理员:
url(r“^admin/”,包括(admin.site.url)),
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>
<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>
    {notifi中notifi的百分比为%}
  • {{notifi.Tipo} {{{notifi.Nombre_}} {{notifi.Fecha_Caducidad_notification}}
  • {%endfor%}
layout.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>
<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>

{%csrf_令牌%}
伊尼西亚·塞西翁。


乌萨里奥 康瑟斯尼娜 录像机 安格尔
问题是,django.contrib.auth.views.login是呈现index.html页面的内容(如您在此处所见):

index.html页面扩展了layout.html,layout.html包括notificaciones.html。这些模板在任何时候都不会传递“notifi”变量(这就是为什么没有显示任何内容-因为django.contrib.auth.views.login不会将任何“notifi”变量传递给模板)。为了完成您想要做的事情,请将URL更改为:

url(r'^$', 'app.views.index', name='Vulpini.co'),
然后在your views.py中,添加以下视图:

def index(request):
 notifi = Notificaciones.objects.all()
 return render_to_response('app/index.html',{ 'notifi' : notifi })
完成此操作后,index.html(它扩展了layout.html,调用notificaciones.html)将可以访问'notifi'变量。然后,在index.html模板中,您可以使用django.contrib.auth.view.login向“/login”发布表单,如下所示:

url(r'^login$', 'django.contrib.auth.views.login', name='Vulpini.co'),
在您的settings.py中,设置:

LOGIN_REDIRECT_URL = '/'
登录后重定向回index.html页面

编辑:由于这是勾选答案,我想指出另一个选项(如chem1st在其答案中所说)是在此处查看上下文处理器:


查看chem1st的答案了解更多信息。

包含和扩展块与将数据从视图传递到模板无关。如果希望能够从视图中获取smth,请显式传递它


您还应该查看,因为它们将允许您在全球范围内获取所需的数据。

这里的一切似乎都正常。显示你的
应用程序/notificacions.html
。那么
url
inclide
block只能插入html,不能插入视图中的数据,因此您应该自己将其绑定到所需的模板。@Alfredhb.q您可以编辑您的帖子以向我们显示app/notificationes.html和您的URL.py文件吗?@user2719875完成后,我编辑了帖子并显示了我的notifications.html和URL。py@Alfredhb.q哪个HTML页面是“{%include'app/notificaciones.html%}是用什么视图编写的?哪个视图呈现html页面?(询问是因为您发布的视图-notifi-呈现app/notificaciones.html,这与上面的模板不同).是layout.html,我正在尝试在layout.htmlok中调用notificaciones.html,这很有效,那么现在我必须制作一个表单?,在layout no?@Alfredhb.q中调用它之后,如果您想让用户登录,那么在layout.html(或任何您想要的地方)中创建一个表单,该表单将把数据发布到“/login”URL(例如)(当用户提交表单时)调用django.contrib.auth.views.login方法。django.contrib.auth.views.login随后将处理登录,并重定向回“/”(如settings.py中的login\u redirect\u URL中所述)。我将再次编辑主问题,您可以看到我的表单。@Alfredhb.q是的,这很好,只需确保将“url(r'^login$,'django.contrib.auth.views.login',name='Vulpini.co')”添加到您的url.py文件中,这样django就知道当您向“/login”发送post数据时该怎么做了现在我遇到了一个问题,我不知道当我提交APAREE表格时这是什么意思。这个错误
Prohibido(403)CSRF