I need the request object in all admin templates. In frontend templates, I can achieve this by rendering the template with RequestContext:
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request)
)
With that, I can access the request object in frontend:
{{ request.path }}
How can I do this for all admin views in Django 1.2?
The request should be available in the admin templates if you have 'django.core.context_processors.request' added to your TEMPLATE_CONTEXT_PROCESSORS in your settings.py
Related
I've some url that I want to render a template without creating a view for each one, but inside I need a form with the csrf tag, so when the user post the form I get no csrf token becouse the template have no RequestContext render on it.
How can I render templates declare in url and use csrf without creating a view for each one
My urls.py
path('path/', TemplateView.as_view(template_name='template.html')),
The error:
CSRF token missing or incorrect.
UserWarning: A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.
"A {% csrf_token %} was used in a template, but the context "
I'm using flatpages to add my content.
I was wondering if there was a way to get the active page so I can style it properly on my navigational bar?
Thanks!
The flatpages app uses RequestContext, which means that details from the request are pushed up through the middleware. You can access these request variables in templates if the request template context processor is enabled. Once you enable this, then all your templates will have access to the request, and from there you can get the current requested URL.
So,
Enable the django.core.context_processors.request template context processor (its not enabled by default). You need to make sure you don't disable the default context processors, so add this to your settings.py:
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',
)
In your template, use {{ request.get_full_path }} or {{ request.path }} depending on what you need.
If your using django flat pages, then you will already have the URL's defined.
EG: url(r'^license/$', 'flatpage', {'url': '/license/'}, name='license'),
Using request.path_info should get you the current page. Is that what you where looking for is there of is it something else ?
<a href="{{ flatpage.url }}" {% if flatpage.url == request.path %}class="active-page"{% endif %}>{{ flatpage.title }}</a>
If you have issues with your request context, you can check Burhan Khalid's answer.
I am using django's template engine to render my email templates.
I have no RequestContext when rendering my email, there for I have no access to my STATIC_URL and cant use the {% url %} template tag.
How can I make it work while I am not generating the email from a view function ?
if you want to add domain/url you may get it from Site model:
message_data.update({'site': Site.objects.get_current()})
I have no idea why you think you need RequestContext for the {% url %} tag. It's a normal tag, you can use it in any template.
And STATIC_URL is just a setting: if you can't get it from the context processor, just pass it in manually into the template context: context['STATIC_URL'] = settings.STATIC_URL, or even use the {% static %} template tag.
I'm doing a website in html and base (where all pages extend) I want
to put a session of social network icons. As this session is base on
html it should be displayed on all pages of the website.
I do not want
to put this session in a static html, I want to do in django using
models. This is already done.
Question: Do I have to put the session of social network icons on each view, or can I make a separate view and all others extend this view?
How can I do this?
Try using an inclusion tag. You can create a function for doing all of the work to create the sessions and then associate that with a particular block of HTML.
templatetags/session.py
#register.inclusion_tag('includes/session_box.html')
def output_session_box(...):
...
return { .. }
The associated template file, includes/session_box.html, would have the HTML like any template.
And then your base.html would have:
{% load session %}
{% output_session_box ... %}
Use RequestContext and a context_processor to inject template variables into every view using RequestContext.
It's as simple as a python function accepting request as an arg, and returning a dictionary to be passed into your template.
https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.RequestContext
def my_processor(request):
return {'foo': 'bar'}
TEMPLATE_CONTEXT_PROCESSORS = (
# add path to your context processor here.
)
I generally have a per-project processor for the basics... It's exactly how django adds {{ user }} or {{ STATIC_URL }} to every template.
I have this path in my urls.py:
archive_index_dict = {
'queryset': News.objects.filter(show=True),
'date_field': 'date',
'template_object_name': 'object_list',
}
...
url(r'^$', 'django.views.generic.date_based.archive_index',
archive_index_dict, name='news_archive_index'
),
Now I want to detect in template if a page is current (this is for menu styling). Neither {{ request.path }} nor {{ request.get_full_path }} work in template.
What should I use instead?
SOLUTION
To get request available in templates I had to add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS. This is not set by default (since django 1.3).
Do you have 'django.core.context_processors.request' context processor set up? Almost all CBV use RequestContext by default