how to add links and images in text field of django admin - python

I am using django to create a blog. In my model I am using a text field for my blog content. But I am unable to insert any image or a clickable link. How to add links(clickable) and insert images?

{% block body %}
{% for blog in blog %}
<h1>{{blog.title}}</h1>
{{blog.text|safe|linebreaks}}
<br>
<h6>{{blog.user}}</h6>
<br>
<br>
{% endfor %}
{% endblock %}

there is no thing like clickable link in Database.you can put link as text and while importing it to your HTML use , and for Images there are two options. either you put your image path name in database or change TextField in models to FileField.

Related

How to add links dynamically in html files in django?

For my project, I'm searching for articles on google news based on keyword input by the user, I want to display these links obtained from the search on my results page.
this is my result.html
{% extends 'base.html' %}
{% block title %}Result{% endblock %}
{% block content %}
<h3>Reported Url</h3>
<div>Post Content: <br>{{content}}</div>
<h3>News articles related to your query:</h3>
<ul>
{% for key, value in articles.items %}
<li>{{value}}</li>
{% endfor %}
</ul>
<div>
Back to Home Page
</div>
{% endblock %}
But the links do not work and I get the page not found error, since these links are not contained in urls.py.
How can I link these urls correctly?
thank you
You cannot execute python code inside Django templates.
Check out this thread: Numeric for loop in Django templates
Fixed the problem I had to replace "./" in the article to the homepage of the site I was refering to.

Text area not reading HTML

I have Django´s Blog APP installed, all working fine, but I need to add posts (via admin) with HTML in the post content field, now, the text area can only read plain text (it doesn´t render HTML).
This is the field:
(models.py)
content = models.TextField()
This is the HTML for this field:
<h6 class="card-text" ><small>{{post.content|slice:":500" |linebreaks |safe}}</small></h6>
Question is: are there special configs for Django/Python in order for the field to render HTML?
The safe tag should already do that... have you tried this:
{% autoescape off %}
{{ post.content }}
{% endautoescape %}
https://code.djangoproject.com/wiki/AutoEscaping
Hope this helps!

How do I choose a category page to be the home page for a Pelican site?

I would like the visitors to my site to land on a category page, not on the default index.html. I want them to see the articles in the News categories before anything else.
Maybe is there a way to tell Pelican to output category/news to index.html? I know this would be possible with normal handwritten pages using the save_as field, but how can I do it with an automatic page generated by Pelican?
Your homepage, the index.html file, is just another template from the DIRECT_TEMPLATES list. It'll depend on your theme exactly how it is generated, but you can always override specific templates locally, or you can create a new template for your homepage to replace it (and optionally redirect the original index.html generated page to a different location).
Either way, you can then generate a section that shows the articles from a single category. All template pages are given the same basic variables, which includes the articles list, as well as a categories list with (Category, list_of_articles) tuples.
The easiest way to get all articles for a single, specific category is to filter the articles list directly with the Jinja2 selectattr filter. selectattr('category', '==', categoryname) is matched both against the category name or the slug (whatever you set categoryname to is converted to a slug for you). So if your category is named News, then both 'News' or 'news' works:
<h2>News:</h2>
<ol id="posts-list" class="hfeed">
{% for article in articles | selectattr('category', '==', 'news') %}
<li><article class="hentry">
<header>
<h1><a href="{{ SITEURL }}/{{ article.url }}" rel="bookmark"
title="Permalink to {{ article.title|striptags }}">{{ article.title }}</a></h1>
</header>
<div class="entry-content">
{% include 'article_infos.html' %}
{{ article.summary }}
<a class="readmore" href="{{ SITEURL }}/{{ article.url }}">read more</a>
{% include 'comments.html' %}
</div><!-- /.entry-content -->
</article></li>
{% endfor %}
</ol>
The above reuses the simple theme article markup. You may want to limit the number of news articles; in that case use the batch(size) filter together with first:
<h2>News:</h2>
<ol id="posts-list" class="hfeed">
{% for article in articles | selectattr('category', '==', 'news') | batch(5) | first %}
<!-- render article, etc. -->
The above takes the first 5 articles with News as the category.
Since the basic theme reuses the index.html template for all of the individual archive pages too (for each category, author or tag page), I'd not override the index direct template here. Instead, I'd create a new homepage template (in the pages directory) and write that to index.html and. You need to add the template in the TEMPLATE_PAGES dictionary, where your template pages should live in a separate directory that is configured not to be treated as articles or pages.
Create a new directory for template pages in your content directory; you'll need to make sure Pelican doesn't try to treat files there as articles, so add it to the ARTICLE_EXCLUDES list. So if all your Pelican your content lives in content/, and you have a file homepage.html in the directory output/templates/, you'd use:
ARTICLE_EXCLUDES = ['templates']
TEMPLATE_PAGES = {
'templates/homepage.html': 'index.html',
}
This will overwrite the default index.html generated for articles, there is no need to remove anything from DIRECT_TEMPLATES but you could do so to avoid generating a file you never keep.
The homepage.html template can make full use of any existing templates in the theme, so you can just extend base.html that most themes will have defined:
{% extends "base.html" %}
{% block content %}
<section id="content">
<h2>Recent news:</h2>
<ol>
{% for article in articles | selectattr('category', 'equalto', 'news') | batch(5) | first %}
<!-- markup for each news item -->
{% endfor %}
</ol>
</section><!-- /#content -->
{% endblock content %}
Instead of overwriting the default index, you can also set INDEX_SAVE_AS to direct the original index.html file elsewhere:
ARTICLE_EXCLUDES = ['templates']
TEMPLATE_PAGES = {
'pages/homepage.html': 'index.html',
}
# move the original article index elsewhere:
INDEX_SAVE_AS = 'all_articles.html'
If you use a theme that doesn't reuse the index.html template for more pages or you want to try to make the template work for in those contexts anyway, then you can override the template used for index instead. To override the default index.html from your theme, create a local directory (overrides perhaps) to put your local version into, then add that directory to the THEME_TEMPLATES_OVERRIDES list in your configuration:
THEME_TEMPLATES_OVERRIDES = ['overrides']
Now when Pelican tries to load the index.html template to render the index direct template, it'll look for overrides/index.html first. So in overrides/ add your own index.html:
{% extends "base.html" %}
{% block content %}
<section id="content">
<!-- put your desired content here -->
</section><!-- /#content -->
{% endblock content %}
A word on pagination: all templates, except for the special per-archive-type pages (categories, authors, tags, periods), are paginated on the full articles list, this isn't something that is further configurable. This means you can't paginate a homepage on a single category of articles.
This means that if you override the index.html template and removed the full article listing, then you may want to remove index from the PAGINATED_TEMPLATES dictionary so it isn't re-generated multiple times to match your article list.

Customize template tags in Django to filter featured_posts in a blog

I have taken a hint from this post
Customising tags in Django to filter posts in Post model
I have created the template tag but I am not sure how to use it in my html. I have a home.html where I want to show three featured post. I am looking for something like {% for post in featured_post %} and then show the post detail.
Also, do I necessarily need to create a featured_posts.html as in the above post because I don't want any extra page for the featured post. I just want them to add on my home page in addition to other stuff.
What I am trying to do is I have created a template tag as under
from django import template
register = template.Library()
#register.inclusion_tag('featured_posts.html')
def featured_posts(count=3):
if Post.is_featured:
featured_posts = Post.published.order_by('-publish')[:count]
return {'featured_posts': featured_posts}
The problem I am facing here is I can't import the Post model from model. My directory structure is somewhat like this:-
I have an app named posts.
Inside that I have models.py and templatetags module and inside the template tag I have blog_tags.py
I couldn't do the relative import.
And then created a new page featured_posts.html as under:-
<ul>
{% for post in featured_posts %}
<li>{{ post.title }} </li>
{% endfor %}
</ul>
Now, I want to use it in my home.html. How can I use it?
Edit:- As mentioned above I could load the models as under:-
from posts.models import Post
home.html
{% load blog_tags %}
{% featured_posts %}
Call your tag. That's it.
or
{% featured_posts count=15 %}
Note, featured_posts here is not the post list (which is iterated in for loop) from context but function name: def featured_posts(count=3). They have the same name in your code and probably this has confused you a little.

Django: Is it possible to call a template from inside another template? [duplicate]

I have a very basic template (basic_template.html), and want to fill in the with data formatted using another partial template. The basic_template.html might contain several things formatted using the partial template.
How should I structure the code in views.py?
The reason I am doing this is that later on the will be filled using Ajax. Am I doing this right?
You can do:
<div class="basic">
{% include "main/includes/subtemplate.html" %}
</div>
where subtemplate.html is another Django template. In this subtemplate.html you can put the HTML that would be obtained with Ajax.
You can also include the template multiple times:
<div class="basic">
{% for item in items %}
{% include "main/includes/subtemplate.html" %}
{% endfor %}
</div>
You can do this using a block. Blocks are a Django Template tag which will override sections of a template you extend. I've included an example below.
basic_template.html
<body>
{% block 'body' %}
{% endblock %}
</body>
template you want to include: (i.e. example.html)
{% extends 'basic_template.html' %}
{% block 'body' %}
/* HTML goes here */
{% endblock %}
views.py:
return render_to_response(template='example.html', context, context_instance)
Doing this will load basic_template.html, but replace everything inside of {% block 'body' %} {% endblock %} in basic_template.html with whatever is contained within {% block 'body' %} {% endblock %}.
You can read more about blocks and template inheritance in the Django Docs
There are mainly 2 ways (2 easy ones)
1:
In base html put
{% include "myapp/sub.html" %}
And just write html code inside your sub.html file
2:
https://docs.djangoproject.com/en/dev/ref/templates/language/#template-inheritance
I just wanted to add differences of extend and include.
Both template and include can use models inserted in current app.
Template is for global usage by your any app. Include is for use in certain apps.
For ex: you want to insert Image Slider to your homepage and about page but nowhere else. You can create Slider app with its own model for convenience and import its model and include in that pages.
If you used template for this example, you would create 2 templates one with slider and everything else other template have.

Categories

Resources