2015-10-20 61 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,因爲它們將允許您全局實現所需的數據。