Incomplete HTML via r.text from Django template with python-requests - python

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)

Related

Django - Would I be able to use template tags for rendering HTML given URL path conditionals?

I have a blog page with the path /articles/. I would like to use conditional statements to reflect HTML renders.
For example, my blog uses a paginator for blog posts. The paginator uses a URL query to retrieve the next page of post objects like so: /articles/?page=2
I would like for the next page to stylize the HTML template differently, hence my reasoning for using a conditional statement.
Here is my template that I use:
{% if request.path == '/articles/' %}
# Render HTML only meant for the above path
{% elif request.path != '/articles/'%}
# Render HTML for paths that are NOT the exact match above, such as pagination URLs,
# search querys, categories filtering, etc...
{% endif %}
My current bug is that I'm only rendering the first statement: if request.path == '/articles/' for everything, even non exact matches.
So this URL path: /articles/?page=2 is rendering: {% if request.path == '/articles/' %} when it should be rendering the elif {% elif request.path != '/articles/'%} due to the not equals sign.
Is the above possible to do with only HTML template tags? I know I can use urls.py and edit views but I'm using wagtail CMS and playing with the template seems like the most easiest thing to do right now.
request.path does not include the '?...' querystring part of the URL, so this is working as designed - the path portion of the URL /articles/?page=2 is /articles/.
Django compiles the querystring into the dictionary request.GET, so one way of doing what you want is to check whether that dictionary is empty or not:
{% if not request.GET %}
request is for the plain URL /articles/
{% else %}
request has a querystring parameter on it
{% endif %}

I want import python code in htm file with django

{% 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/

Django - Passing argument through href for URL dispatcher?

So I am working on a website where at some point I do a search and list a list of textbooks on the page. From there I want the user to be able to click on a textbook and each textbook will have it's own details page. I have been trying to work with Django's URL dispatcher but I am having difficulties. Code and description below.
results.html
<table class="table">
{% for items in results %}
<tr><td>{{items.textbook_name}}</td><td>{{items.class_name}}</td><td>{{items.author}}</td><td>{{items.isbn}}</td><td>></td></tr>
{% endfor %}
</table>
Views.py
def textbook(request, text_name):
return render_to_response(
'textchange/textbook.html',
locals(),
context_instance=RequestContext(request)
)
Urls.py
url(r'^results/(?P<text_name>\w+)/$', views.textbook, name="textbook"),
From my understanding I thought I was passing the items.textbook_name as text_name to urls from the html and then views would be called with text_name as an argument but it is not working. I might be a little backwards here. I need the textbook_name from the textbook the user clicks on so on the details page I can display all it's information from the database.
Thanks.
Quick explanation.
Let's say that your url doesn't need text_name parameter, so that:
{% url 'textchange:textbook' %}?text_name={{items.textbook_name}}
will result in:
/results/?text_name=some-name
text_name won't get passed to url tag, it was simply glued on the end of url.
To pass text_name into url, and build proper url, you should do that:
{% url 'textchange:textbook' text_name=items.textbook_name %}
and it will result in url:
/results/some-name/

linking outside of Django site in dev mode

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.

Generating a url with the same GET parameters as the current page in a Django template

I have a certain link to a url in a Django template. I would like to grab all the GET parameters of the current page url and add them to the url of the template's link. The current page might have zero GET parameters.
Include the django.core.context_processors.request context processor in your settings.py, then use the request object in your template's links:
<a href="{% url 'my_url' %}?{{ request.META.QUERY_STRING }}">
This will cause links from a page without any GET variables to have a trailing ? but that's harmless. If that's not acceptable, you could test for them first:
<a href="{% url 'my_url' %}{% if request.META.QUERY_STRING %}?{{ request.META.QUERY_STRING }}{% endif %}">
you could pass request.META['QUERY_STRING'] to the template.
You can grab the get params before you render the template and pass them to the template and render them on the correct link.
You could also build a query string from request.GET
The GET parameters of the current request are stored in HTTPRequest.Get.

Categories

Resources