Newbie : Help for using RequestContext

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Newbie : Help for using RequestContext

Mickael.barbo
Hello to all Django users :-)

I'm new to Django and this is my first post.

I'd like to implement breadcrumbs on all my pages.

My strategy is to create a middleware and to use RequestContext to
add a dict to the context of each request then use the context in my html

My problem is that I don't understand how to link RequestContext to
my request. Hereafter my code :

my_middleware.py
from django.template import RequestContext


class SimpleBreadcrumbs(object):
   
def __init__(self, get_response):
       
self.get_response = get_response
       
# One-time configuration and initialization.


   
def __call__(self, request):
       
# Code to be executed for each request before
       
# the view (and later middleware) are called.

        request_context
= RequestContext(request)
        request_context
.push({"level0": "Adrian", "level1": "Thomas"})

        response
= self.get_response(request)


       
# Code to be executed for each request/response after
       
# the view is called.

       
return response


my_template.html

{% extends 'pricing/body.html' %}

{% block content %}

{{ level0 }}

{% endblock content %}


What I'm doing wrong ?

Thx for your help

Micka
Django newbie
;-)





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6cf0e65a-2b48-4bbb-a8a7-0351252e49f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Newbie : Help for using RequestContext

Andréas Kühne
Hi Micka,

And welcome to django.

Regarding how to add things in middleware - you can add the information to the session like this:

class SimpleBreadcrumbs(object):
    
def __init__(self, get_response):
        
self.get_response = get_response
        
# One-time configuration and initialization.


    
def __call__(self, request):
        
# Code to be executed for each request before
        
# the view (and later middleware) are called.

        request.session['breadcrumbs'] = {"level0": "Adrian", "level1": "Thomas"}

        response = self.get_response(request)


        
# Code to be executed for each request/response after
        
# the view is called.

        
return response


Then in your templates you can do this:

{% extends 'pricing/body.html' %}

{% block content %}

{{ breadcrumbs.level0 }} / {{ breadcrumbs.level1 }}

{% endblock content %}

Regards,

Andréas

2018-01-10 0:42 GMT+01:00 <[hidden email]>:
Hello to all Django users :-)

I'm new to Django and this is my first post.

I'd like to implement breadcrumbs on all my pages.

My strategy is to create a middleware and to use RequestContext to
add a dict to the context of each request then use the context in my html

My problem is that I don't understand how to link RequestContext to
my request. Hereafter my code :

my_middleware.py
from django.template import RequestContext


class SimpleBreadcrumbs(object):
   
def __init__(self, get_response):
       
self.get_response = get_response
       
# One-time configuration and initialization.


   
def __call__(self, request):
       
# Code to be executed for each request before
       
# the view (and later middleware) are called.

        request_context
= RequestContext(request)
        request_context
.push({"level0": "Adrian", "level1": "Thomas"})

        response
= self.get_response(request)


       
# Code to be executed for each request/response after
       
# the view is called.

       
return response


my_template.html

{% extends 'pricing/body.html' %}

{% block content %}

{{ level0 }}

{% endblock content %}


What I'm doing wrong ?

Thx for your help

Micka
Django newbie
;-)





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6cf0e65a-2b48-4bbb-a8a7-0351252e49f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCem5X%2B4ess5bexkej_fs_TmG1vHMbxOzJco5bzU-sQyZw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Newbie : Help for using RequestContext

Mickael.barbo
Oh, Thank you Andréas for your kind attention and your reactivity.

Nice, I didn't know about request.session... And thanks to you, now I Know :-)

Just for archive, I found a way with context_processors here (old django version, but I adapt to django 2) :
http://apprendre-python.com/page-django-context-processor-template

Very happy to join Django community :D

Micka

Le mercredi 10 janvier 2018 09:36:46 UTC+1, Andréas Kühne a écrit :
Hi Micka,

And welcome to django.

Regarding how to add things in middleware - you can add the information to the session like this:

class SimpleBreadcrumbs(object):
    
def __init__(self, get_response):
        
self.get_response = get_response
        
# One-time configuration and initialization.


    
def __call__(self, request):
        
# Code to be executed for each request before
        
# the view (and later middleware) are called.

        request.session['breadcrumbs'] = {"level0": "Adrian", "level1": "Thomas"}

        response = self.get_response(request)


        
# Code to be executed for each request/response after
        
# the view is called.

        
return response


Then in your templates you can do this:

{% extends 'pricing/body.html' %}

{% block content %}

{{ breadcrumbs.level0 }} / {{ breadcrumbs.level1 }}

{% endblock content %}

Regards,

Andréas

2018-01-10 0:42 GMT+01:00 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="lKaAsBBCAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">Mickae...@...>:
Hello to all Django users :-)

I'm new to Django and this is my first post.

I'd like to implement breadcrumbs on all my pages.

My strategy is to create a middleware and to use RequestContext to
add a dict to the context of each request then use the context in my html

My problem is that I don't understand how to link RequestContext to
my request. Hereafter my code :

my_middleware.py
from django.template import RequestContext


class SimpleBreadcrumbs(object):
   
def __init__(self, get_response):
       
self.get_response = get_response
       
# One-time configuration and initialization.


   
def __call__(self, request):
       
# Code to be executed for each request before
       
# the view (and later middleware) are called.

        request_context
= RequestContext(request)
        request_context
.push({"level0": "Adrian", "level1": "Thomas"})

        response
= self.get_response(request)


       
# Code to be executed for each request/response after
       
# the view is called.

       
return response


my_template.html

{% extends 'pricing/body.html' %}

{% block content %}

{{ level0 }}

{% endblock content %}


What I'm doing wrong ?

Thx for your help

Micka
Django newbie
;-)





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="lKaAsBBCAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="lKaAsBBCAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@....
Visit this group at <a href="https://groups.google.com/group/django-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;">https://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/6cf0e65a-2b48-4bbb-a8a7-0351252e49f9%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-users/6cf0e65a-2b48-4bbb-a8a7-0351252e49f9%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-users/6cf0e65a-2b48-4bbb-a8a7-0351252e49f9%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-users/6cf0e65a-2b48-4bbb-a8a7-0351252e49f9%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e6de4d19-7778-4988-abbf-3906a134bc42%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.