简体   繁体   English

包含块未显示在 Django 模板中

[英]Include block is not showing in django template

Well, i've designed something but i am not sure, how to implement it.好吧,我已经设计了一些东西,但我不确定如何实现它。

models.py模型.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视图.py

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

Now I want to show the notification in a header in a lightbox, then in my layout.html, where header, footer and etc. are called.现在我想在灯箱的标题中显示通知,然后在我的 layout.html 中,在那里调用页眉、页脚等。 But when I call notification, it does not appear.但是当我调用通知时,它不会出现。

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

Can someone explain if I can call the notification from views or should it be done somehow else?有人可以解释我是否可以从视图中调用通知还是应该以其他方式完成?

URL.PY网址.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通知.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>

Login Form Inside layout.html登录表单里面 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>

The problem is, django.contrib.auth.views.login is what renders the index.html page (as you can see from here):问题是, django.contrib.auth.views.login 是呈现 index.html 页面的内容(如您所见):

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

The index.html page extends layout.html and layout.html includes notificaciones.html. index.html 页面扩展了 layout.html,而 layout.html 包含了 notificaciones.html。 At no point do these templates get passed the 'notifi' variable (that is why nothing shows up - because django.contrib.auth.views.login does not pass any 'notifi' variable to your template(s)).这些模板在任何时候都不会传递“notifi”变量(这就是为什么什么都没有显示的原因 - 因为 django.contrib.auth.views.login 不会将任何“notifi”变量传递给您的模板)。 In order to accomplish what you want to do, change the URL to this:为了完成您想要做的事情,请将 URL 更改为:

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

and then in your views.py, add this view:然后在您的 views.py 中,添加此视图:

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

Once this is done, then index.html (which extends layout.html which calls notificaciones.html) will have access to the 'notifi' variable.完成此操作后,index.html(它扩展了调用 notificaciones.html 的 layout.html)将可以访问 'notifi' 变量。 Then in your index.html template, you can make a form post to "/login" which uses django.contrib.auth.view.login, like this:然后在您的 index.html 模板中,您可以将表单发布到使用 django.contrib.auth.view.login 的“/login”,如下所示:

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

and in your settings.py, set this:并在您的 settings.py 中设置:

LOGIN_REDIRECT_URL = '/'

to redirect back to the index.html page after logging in.登录后重定向回 index.html 页面。

Edit: Since this is the checked off answer, I'd like to point out that another option (as chem1st said in his answer), would be to have a look at context processors here: https://docs.djangoproject.com/en/1.7/ref/templates/api/#writing-your-own-context-processors编辑:由于这是已勾选的答案,我想指出另一个选项(正如 chem1st 在他的回答中所说),将在这里查看上下文处理器: https ://docs.djangoproject.com/ en/1.7/ref/templates/api/#writing-your-own-context-processors

View chem1st's answer for more information.查看 chem1st 的答案以获取更多信息。

Include and extends blocks do nothing with passing the data from view to a template. Include 和 extends 块对将数据从视图传递到模板没有任何作用。 If you want to be able to get smth from a view, pass it explicitly.如果您希望能够从视图中获取 smth,请显式传递它。

You should also have look at context processors , since they will allow you to globally achive the data you want.您还应该查看上下文处理器,因为它们将允许您全局获取所需的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM