ExpressionEngine content template - templates

i have created channel called "news" and the channel field.
i have created template group called "news".
in news template group, there are "index" and "single" template.
what i want to do is when user go to url mydomain.com/news, it will show all the news with template from "index" template,
but if user go/ click to url mydomain.com/news/first-post, it will show the post with template from "single" template.
how can i achieve this?
i have tried to choose "single" template from "page" tab when i publish new content, but it didn't work.
the content url still show the "index" template
thanks.

URL routing in ExpressionEngine is as follows:
http://www.yourdomain.com/template-group/template/entry-url-title
It makes sense from an EE perspective that the news template is being shown when you go to mydomain.com/news/first-post. EE is looking for a template called first-post in the news template group. You can fix this in one of two ways:
1) Change the url you're looking for to mydomain.com/news/single/first-post. This should show your post.
2) If you don't want to show "single" in the URL, setup a condition in the main news template as follows:
{if segment_2}
{embed="news/single"}
{if:else}
... rest of your template code
{/if}
To keep this template cleaner, you might even want to create a news/main template so you can
{if segment_2}
{embed="news/single"}
{if:else}
{embed="news/main"}
{/if}
Up to you.

Related

how to remove html tags in django template on browser while showing to user

As shown in figure i used {{options|safe}} for rendering options in my django 3.0 polls application even though it is rendering like that and i don't know how to remove the tags from rendered string, thanks for help in advance
regarding tag error
To remove tags, I would recommend using Mozilla's bleach library.
In order to remove tags only in the front-end, not the data itself, you can easily create a custom template filter and clean the tags inside it.
Another cool idea would be to have list of enabled HTML tags that can be used (like making text bold with <b>...</b>) and then render the input as a valid html:
{{ options|remove_tags|safe }}
Example for a custom template filter:
#register.filter
def remove_tags(value):
return bleach.clean(value, tags=["b", "i"])

Can we enter html code in a field in models in django?

I am making a small static website in which I have a template in which I tend to show the privacy policy terms of usage etc. I currently don't have any matter for it and tend to add it in future after deploying the site on server. I wanted to know that if I can in future add the matter on that page through a model i.e I create a model with two fields privacy policy , terms and in and pass it to the template as context in views.py . But I have a concern that the fields will have several headings which I will have to display in bold , so is there any way that I can pass html tags in model field and when I render it in my template as {{privacy}} the part I want in bold or any other style comes as that style.
So is there any way that I can pass html tags in model field and when I render it in my template as {{privacy}} the part I want in bold or any other style comes as that style.
Yes. You only need to tell the Django template engine not to escape the characters (for example translate < to <). You can do this with the |safe template tag [Django-doc]:
{{ privacy|safe }}

Mezzanine: Editable tag inside header/footer template

I want to be able to use {% editable something %} inside of a layout template or a template that is included in various pages. For example a company slogan in the header or a text inside of page_menu.
I want to edit the value only on one place of administration (I don't want to have it duplicated over all pages models).
What is the best way to do this?
As commented above, the something arg can be any model instance - so all you'll need it a template tag or possibly context processor for getting the instance into every template, then voila.

Django's equivalence of ASP.NET UserControl

If anyone here is ASP.NET pro, you might know what I mean by user control. I wish to create a similar one in django instead.
So, my problem is that I have several pages in my website, but I need a search bar to appear in every pages. Since I require the views.py to operate this search bar, I cannot do a simple method of
{% include 'something.html' %}
Therefore, can anyone suggest how can I do it?
There are a couple of ways to accomplish what you're wanting to do:
Context Processors
Template Tags
Context Processors can augment the template context with values, regardless of which template is loaded. They are akin to filters in Rails.
Template Tags, like Context Processors, can accomplish anything you can do in Python, but are implemented at the template level.
If you need something to be present on every template, one of the simplest ways to accomplish this is with an inclusion tag, which can also accept values passed to it. An inclusion tag could be implemented at your highest level template, a.k.a your MasterPage, and as long as you don't put it in a block and override it, it would appear on every page that includes that template in its inheritance chain.
If it's just something you want to include on every page, and it doesn't need to do any processing, you should just be able to place the code you want in the top-most template and have subsequent templates inherit that.
I typically have a "base.html" template that all of my templates inherit from. If I need something to be in every page, I put it there. If it's something I want there by default, but want to be able to augment it in subsequent templates, I will place it into a block. That block will let you include or override its default content.
I know this post is kind of old but I just came across it and found a kind-of-solution that works. I call it kind-of-solution because it is a workaround.
I have a few different sites on which I want to display logging information. This display always looks the same (it has the same html) and has the same database table and model class behind it.
My solution/workaround uses the django filters:
in views.py I put the list of log-entries in the context
context = {'list_log': Log.objects.filter(condition = True) }
template = loader.get_template('my_html_file.html')
return HttpResponse(template.render(context, request))
in my_html_file.html I use a custom filter
{{ list_log|get_log_uc|safe }}
in the filters.py I load another html file with this custom filter
#register.filter
def get_log_uc(list_log):
template = loader.get_template('user_control_log.html')
context = { 'list_log' : log }
return template.render(context)
in user_control_log.html I have the user control equivalent html
{% for log in list_log %}
<p>log.something</p>
{% endfor %

ExpressionEngine 1.6.9: display DIV at particular pages generated by same template

I'm a EE newbie. I have a template for subpage. all subpages use same subpage template. But for some sub pages I need to put an extra div (kinda info box), how can I put a condition? do I have to create a separate template only for a small div difference?
urls are consistent, so if i can make a url check and display div for right urls, it would work out for me, but can I put any php if {} condition into template?
Appreciate helps so much!!!
You can use ExpressionEngine conditionals.
See the documentation here:
http://expressionengine.com/legacy_docs/templates/globals/conditionals.html
Based on your description, I think you could do this based on the URL. You can read URLs using segment variables. So if your URL is http://www.foo.com/bar/test and you only want to show the box on the /bar/test page, use a conditional like this:
{if segment_2 == 'test'}
<div id="foobar">Lorem Ipsum</div>
{/if}