linking outside of Django site in dev mode - python

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.

Related

How to use an if statement in flask to render code only on a specific url

I am sorry if this has been asked but I was unable to find the answer after searching stackoverflow.
Basically I am trying to use an if statement in my jinja2 template so that if the /login page is the current url, the modal for login is automatically opened. Reason is that every public page has the login modal, but when a url only available to authenticated users is loaded, I have it redirecting to a login (without having a unique page for /login, so /login shares the homepage template). So the modal needs to automatically open when on /login.
This is what I have been trying in my template:
{% if request.url == url_for('login') %}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$("#login").modal('show');
});
</script>
{% endif %}
If I add that code to my template, without the if statement, it does work fine and every page load loads the page with the modal opened. My if statement is obviously the problem as none of the pages using that template including /login load with the modal open. I have tried many things to no avail. Any help with a proper if statement or perhaps a better way to achieve my goal would be much appreciated. Thanks in advance and apologies for the long winded explanation.
Thanks to PGHE who commented on my question, the answer was very easy to find.
When inspecting the values of {{ request.url }} and {{ url_for('login') }} I had the following result:
/login
127.0.0.1:5000/login
So I changed request.url from my if statement to request.path which got it to work perfectly.
Thank you PGHE

request.resolver_match.kwargs only works while inside for loop?

I'm using request.resolver_match.kwargs with django to grab the url param for a view I'm trying to link to. When I use it as so
Print this page
I get a noreversematch error. however if I put it inside a for loop it works as so
{% for uid in request.resolver_match.kwargs.uid %}
Print this page
{% endfor %}
With the obvious side effect of having as many links as there are characters in the url param. I'm not sure why it works in the for loop but not as a standalone.

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/

Sending messages using Django Postman

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.

Django blog, up-down voting feature using github source

I'm trying to include a youtube like up and down voting feature for my blog. I have used pip install django-updown to install this to my project. I have followed all the instructions there.
I have included updown to INSTALLED_APPS, included rating = RatingField() to my models.py file and also included the url as described. But, the problem is, I can't see the voting system in my blog? I'm guessing it is something to do with the template file since nothing has been mentioned about it.
I have the following commenting form in my template and would like the up-down vote to be shown before this. Can anyone help me out? Thanks :)
<div class="comment_form">
{% if user.is_authenticated %}
{% get_comment_form for object as form %}
.....
{% else %}
<p>Please log in to leave a comment.</p>
{% endif %}
</div>
Maybe there is a problem with my urls.py file?
This is the url for my individual blog post:
url(r'^book/(?P<pk>\d+)$', BookDetail.as_view(), name='book-detail'),
Following the documentation mentioned in the source documentation, I included:
url(r'^book/(?P<object_id>\d+)/rate/(?P<score>[\d\-]+)$', AddRatingFromModel(), {
'app_label': 'bookBlog',
'model': 'Book',
'field_name': 'rating',
name="book_rating"),
From django-updown github:
To submit a vote just go to video/<id>/rate/(1|-1).
If you allowed users to change they're vote, they can do it with the same url.
Try following that URL directly and check if your votes are being added or not. If it is working, i guess you could just set some links to that view or something (or send some ajax requests).

Categories

Resources