【问题标题】:Include block is not showing in django template包含块未显示在 django 模板中
【发布时间】:2016-01-19 12:59:34
【问题描述】:

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

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>

【问题讨论】:

  • 这里似乎一切正常。显示您的app/notificaciones.htmlurls 呢? inclide 块只能插入 html,不能插入视图中的数据,因此您应该自己将其绑定到所需的模板。
  • @Alfredhb.q 你能编辑你的帖子向我们展示 app/notificaciones.html 和你的 URLs.py 文件吗?
  • @user2719875 完成,我编辑了帖子并显示了我的 notificaciones.html 和 URLs.py
  • @Alfredhb.q 哪个 HTML 页面是 "" 写在?什么视图呈现 html 页面? (询问是因为您发布的视图 - notifi - 呈现 app/notificaciones.html,这与上面的模板不同)。
  • 是 layout.html,我正在尝试在 layout.html 中调用 notificaciones.html

标签: javascript python html django django-models


【解决方案1】:

问题是,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”变量传递给您的模板)。为了完成您想做的事情,请将 URL 更改为:

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

然后在你的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 在他的回答中所说),将在这里查看上下文处理器:https://docs.djangoproject.com/en/1.7/ref/templates/api/#writing-your-own-context-processors

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

【讨论】:

  • 好的,这行得通,那么现在我必须制作一个表格?,然后在布局中调用它?
  • @Alfredhb.q 如果你想让用户登录,那么在 layout.html(或任何你想要的地方)中创建一个表单,该表单对“/login” URL 的操作(例如
    )。然后表单会将数据发布到调用 django.contrib.auth.views.login 方法的“/login”(当用户提交表单时)。然后 django.contrib.auth.views.login 将处理登录,并将重定向回“/”(如 settings.py 中的 LOGIN_REDIRECT_URL 中所述)。
  • 我将再次编辑主要问题,您可以看到我的表单看起来如何。
  • @Alfredhb.q 是的,这很好,只要确保添加 "url(r'^login$', 'django.contrib.auth.views.login', name='Vulpini.co' )," 到您的 urls.py 文件中,这样 Django 就知道当您将发布数据发送到“/login” URL 时该做什么。
  • 现在我有一个问题,当我提交表单时我不确定这意味着什么,这个错误Prohibido (403) CSRF verificacion fallida. Solicitud abortada
【解决方案2】:

Include 和 extends 块对将数据从视图传递到模板没有任何作用。如果您希望能够从视图中获取 smth,请显式传递它。

您还应该查看context processors,因为它们可以让您在全球范围内获取您想要的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 2011-11-02
    • 1970-01-01
    • 2018-12-11
    • 2015-11-09
    • 2013-06-23
    • 2022-01-03
    相关资源
    最近更新 更多