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.
Related
i have html component that i want to include in my template that has a link as below
<a href="{{url_name}}" ........
in my template i am trying to pass the url_name using the with tag
{% include 'components/buttons/_add_button.html' with url_name="{% url 'equipments:equipment-add' %}" reference_name='equipment' %}
this seems not to work as i get the following error
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '"{%' from '"{%'
this works though
using the hardcoded url works however i wanted to implement this using url names for flexibility
{% include 'components/buttons/_add_button.html' with url_name="equipments/equipment/add" reference_name='equipment' %}
urls.py
app_name = "equipments"
....
path("equipment/add/",views.EquipmentCreateView.as_view(), name="equipment-add",
),
...
can i use a custom tag to pass this url name to my component template
I solved this by using a custom filter in Django as:
#register.filter
def get_url(url_name):
return reverse(url_name)
I now pass the url_name in my template as:
{% include 'components/buttons/_add_button.html' with url_name="equipments:equipment-add" reference_name='equipment' %}
Then in my Html component I use:
<a href="{{url_name|get_url}}"...
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'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 am trying to put a login form in every page in my web that uses django.contrib.auth.views.login. I created a templatetag in templatetags/mytags.py, where I define a function called get_login wich looks like this:
#register.inclusion_tag('registration/login.html', takes_context=True)
def get_login(context):
...
return {'formLogin': mark_safe(AuthenticationForm())}
...and in base.html:
{% load mytags %}{% get_login %}
The problem now is that the template (registration/login.html) doesnt recognize {{ formLogin.username }},{{ formLogin.password }}... and so on.
What am I missing?
mark_safe returns an instance of django.utils.safestring.SafeString, not a form, so those lookups will fail. I don't think there's anything wrong with directly returning the form (that's what all the generic views in django.contrib.auth do when populating templates, for instance). Just change your return statement to
return {'formLogin': AuthenticationForm()}
and it should work.
I have project in Django 1.3. In order to show username in all pages I use such tags in base.html
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}.
loggout</p>
{% else %}
loggin
{% endif %}
But if I dont return context_instance=RequestContext(request) from view value of user in template is empty. The 'django.contrib.auth.context_processors.auth' is included to TEMPLATE_CONTEXT_PROCESSORS.
Is it possible automaticaly include user to all templates?
since django 1.3. use shortcuts.render function and dont warry about requestcontext including to your views
You've given the answer yourself. As long as you use a RequestContext, it will be included in all templates.
If you really find that too much work, you could use the (new in 1.3) TemplateResponse class.
Or simply create a context processor. See
http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors
Put this in context_processor.py
def root_categories(request):
return {
'user': request.user,
}
in settings.py add the context processor.
now in your template try: {{ user }}