{% for post in posts %}
{{post.name}}
{{post.content}}
{% endfor %}
I created html file and i'm trying to use python code in html but {{}} {%%} tags not working. It show me my code like a html p tag
Only i see this on browser not working codes. where is the problem library?
According to the documentation is correct the way you did, maybe the error is in the view, see this link https://docs.djangoproject.com/en/2.1/ref/templates/builtins/
Related
I'm using python-requests to get HTML from a page and later extract some links from it.
Here's the modified part of the function:
# views.py
import requests
def my_function(request):
page = requests.get("http://some/url")
html = page.text
print(html)
When I execute my_function via a {% url 'url_name' %} in the template file, the printed page.text doesn't contain part of the HTML code that should be visible on the page. It appears that the code that is missing is that is within {% block content %} {% endblock content %} in my template file.
What is the reason for this and how can I fix it?
(I'm using Python 3.8.2, Django 3.0.6, requests 2.23.0)
I'm using auto generated HTML which has been saved to a file and then read in again to use as part of a page in a django template.
In order to do this I have used:
{% autoescape off %}
{{ my_html }}
{% endautoescape %}
However, in the my_html variable, I have some static content. This comes in the form of something like this:
<img src="{% static "img/tree_report.png" %}" width="12px" height="12px"/>
My issue is that the static content is not displayed. I get:
http://127.0.0.1:8000/%7B%%20static 404 (in the browser error report)
I read something about get_static_prefix in another question but that doesn't solve my problem because I just get this instead:
GET http://127.0.0.1:8000/%7B%%20get_static_prefix%20%%7Dimg/tree_report.png 404 (Not Found)
I also tried endautoscape turning on and off periodically in my_html in the saved HTML variable. That also didn't work.
Should I be autogenerating the development and production static files paths for my_html or is there a more elegant solution to this problem?
Any suggestions are most welcome.
Thanks.
I am trying to use Django-Postman and have gotten as far as being able to see the templates on the webpage after I press the link but I don't know how send messages works. According to the write view there should be a form loaded but all I get is the links to the other pages in the template. If someone could explain how to get this to work it would be fantastic.
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav ">
<li>MyCourse</li>
<li>Timetable</li>
<li>logout</li>
<li>Inbox</li>
</ul>
</div>
Recently had to set up postman myself, and based just off your snippet, I'm going to assume you wrote the ul element yourself.
So, if that's the case, postman actually expects to handle all of that for you. According to the docs, you need to create a base.html template inside your own template directory. A few blocks are expected to be present inside this template, namely {% title %} (text it will add to the page's title), {% extrahead %} (some extra js and css), {% content %} (would contain the missing forms you're looking for) and {% postman_menu %} (the menu links automatically generated by postman).
You could always create the menu links yourself, but I'd suggest you create a /postman folder in your app's template folder, then copy the base.html from the installed postman app (this contains the code for how to layout the ul). Just a little more django-esque, and usefull if you need to fiddle with the names of the template tags, etc, but that's up to you.
Hope this helps, happy coding.
I'm testing a django site on the local server: http://127.0.0.1:8000/
The sites internal links all work great, even the static links. However, when I try to link to an outside link with say google as text inside a text field of my blog model it doesnt render the link correctly. That blog model is then passed through as |safe (so that the html is rendered) to the template, and the link is instead trying to append everything to the static root:
http://127.0.0.1:8000/blog/view/http://google.com
Anyone know how to keep my static links working, but still have links that go outside of the site?
EDIT:
For example, here is a blog post that is stored in a TextField() from the admin, inside my blog app. The blog post has some links. The link to the /static/mytextfile works fine as it appends that to the http://127.0.0.1:8000/. However, the github link isnt working as it attempts to append the github link to http://127.0.0.1:8000/ and thus the outputted html creates "http://127.0.0.1:8000/http://github.com/":
<p><b>The Code</b><br>
<a href=”http://github.com/”>GitHub</a>
<p><b>Example Outputs</b>
<br>a text file
Here's the 404 error that I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/view/%E2%80%9Dhttp://github.com/%E2%80%9D
EDIT 2:
This is how I have been 'escaping' the html filter. Up until now it has worked fine at leaving my <p> etc alone. It is not however leaving my href links alone!
{% autoescape off %}
{% block content %}
<p>{{ post.body|safe }}</p>
{% endblock %}
{% endautoescape %}
Something seems to be wrong with your double quote characters for the GitHub link.
Instead of:
<p><b>The Code</b><br>
<a href=”http://github.com/”>GitHub</a>
Try:
<p><b>The Code</b><br>
GitHub
I think you should try google.
This is a snippet of my code.
soup=BeautifulSoup(html_document)
tabulka=soup.find("table",width="100%")
dls=tabulka.findAll("dl",{"class":"resultClassify"})
tps=tabulka.findAll("div",{"class":"pageT clearfix"})
return render_to_response('result.html',{'search_key':search_key,'turnpages
':tps,'bookmarks':dls})
I checked the dls, it is a dict contains only one html label
<dl>label contents contains some <dd> labels</dl>
But after pass dls to render_to_response the result is not correct.
The corresponding template code in result.html is:
{% if bookmarks %}
{% for bookmark in bookmarks %}
{{bookmark|safe}}
{% endfor %}
{% else %}
<p>No bookmarks found.</p>
{% endif %}
The output result html contains a python dictionary format like this:
[<dd>some html</dd>,<dd>some html</dd>,<dd>some html</dd>,...]
This appears in the output html. That is very strange. Is this a bug of renfer_to_response?
Well, dls is a python list containing the text of all the matching elements. render_to_response doesn't know what to do with a list, so it just turns it into a string. If you want to insert all the elements as HTML, try joining them into a single piece of text like so:
dls = "".join(dls)
Note that by doing so you are pasting live HTML from some other source into your own page, which is potentially unsafe. (What happens if one of the dds contains malicious Javascript? Do you trust the provider of that HTML?)
You have to use a RequestContext instance when rendering templates in Django.
say like this
return render_to_response('login.html',{'at':at}, context_instance = RequestContext(request))
for this to use, you need to import as follows:
from django.template import RequestContext
Hope this works for you. :)