Correct way to use Facebook Like plugin in Django - django

I am using the following Facebook code to show the Like plugin when iterating through my list of Post objects.
<fb:like href="{% url post post.id %}" layout="button_count" show_faces="false" width="450" font=""></fb:like>
The resulting HTML is as such:
<fb:like href="/9/" layout="button_count" show_faces="false" width="450" font=""></fb:like>
However, when I click the Like button, my FB profile says that I Liked www.facebook.com/9/ instead of my own domain name.
What did I do wrong?
Thank you!

The problem is that {% url post post.id %} returns an absolute url path without the domain. Facebook plugins need the full url with host. To make your page likable you also need to pass your domain somehow to the template. Lets assume you createa template variable host via request.get_host() in your views and pass it to your template then the url statement could look like this:
<fb:like href="http://{{host}}{% url post post.id %}" layout="button_count" show_faces="false" width="450" font=""></fb:like>

Related

Place image on page that users can refer too using "src" in an "img" tag (Django)

How do I place an image that people can refer to i.e via a link just like this ?
If I just place a normal <img src="{% static 'media/my_img.png' %}"> in my template and inspect the site, the source is static/media/my_image.png.
I want to be able to refer to specific images from my webpage in an email using html like<img src="my_page.com/logos/my_image.png>
You need to add your site url in the your context
If it is your webpage, you can use: {{ request.get_host }}
So it will become: <img src="{{ request.get_host }}{% static 'media/my_img.png' %}">
When generating an email just make sur you also provide the host or the request object when generating the email content and use it in the same way.

How to add link to a certain section in the rendered page in Django?

I render a page with additional key parameter added to the URL. For example:
<form action="{% url 'Page' %}">
<select name="order">
<option value='up'> Up</option>
<option value='down'> Down</option>
</select>
<button type="submit" >Submit</button>
</form>
And in the views.py file, I render the template file based on the key parameter appears in the request path_info.
return render(request, 'index.html', context)
Every time I render this template, the page always starts from the top, however, I want the rendered page to go to a certain section of the template file, like when you do tag in HTML to link between sections.
Link
It is possible for me to add different links to different section based on the key parameters in the URL (Like "/page/?order=up" go to id="up" and "/page/?order=down" go to id="down") so that the rendered page doesn't always start at the top?
Thank you very much!
As you didn't mention which method you had tried, I'll suggest you go look at this thread.
<!-- something like -->
My link

Forbidden 403 : CSRF Validation failed error in Firefox, not in chrome

I have a webpage with more than 1 form with POST. I have included {% csrf_token %} in each of the forms.
<form class="form-horizontal clearfix" role="form" id="Form1" method="post"> {% csrf_token %}
In my view I have used bot ensure_csrf_cookie and csrf_protect decorators
#ensure_csrf_cookie
#csrf_protect
#operation('monitor')
def monitor(request, **kwargs):
The first POST request fetches some details from the backend and displays it in the UI. After that, some data is requested from the user and then the second form is submitted.
In firefox: When I first load the page and after the first post the csrftoken is X. For the second post request also, the csrftoken cookie is the same. But once the error is thrown, the csrf cookie changes to a different value. If I refresh the page after that, csrftoken remains the same and if I post the request again it succeeds. I have also verified that the form has the hidden value csrfmiddlewaretoken. This matches the cookie. I don't see any 404 for favicon when the page is loaded. This was there before. But I fixed that and chrome started working.
In chrome: This is working.
Any pointers on how to solve this?
It was the favicon.ico not found error. I added this in my base.html.
<link rel="shortcut icon" type="image/png" href="{% static "tools/ico/favicon.ico" %}"/>
Reference: https://code.djangoproject.com/ticket/28488#comment:22

Using an embedded Iframe in Django?

How do I use an Iframe in Django. Currently its searching for the src url in my own files. How do I tell it to not look in my files but to look at the url?
Currently I'm storing the embed code minus the tag in a database and then trying to dynamically generate it in my template.
games.embed = 'allowtransparency="true" width="485" height="402" src="//scratch.mit.edu/projects/embed/82384372/?autostart=false" frameborder="0" allowfullscreen'
{% extends 'code_games/base.html' %}
{% block content %}
<div class="game">
{{games.title|linebreaks}}
<iframe '{{ games.embed }}'></iframe>
</div>
{% endblock %}
The iframe itself shows up on my page but the contents of it don't.
The request URL per the error:
Request URL: http://chacemcguyer.pythonanywhere.com/games/1/%22/scratch.mit.edu/projects/embed/82384372/?autostart=false%22
You can see that its searching for the url in my site. How do I get around that?
The error also says:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
Then it shows all of my urls from settings.py
I found a solution!
What I had to do was make sure that the iframe content which was coming from my database was |safe
I changed:
<iframe src='{{ games.embed }}'></iframe>
to
<iframe src='{{ games.embed|safe }}'></iframe>

How to include a link to reset password and register in Django views?

In my Django view, I use the following tags for logging in and logging out:
<href="{% url 'login' %}">
<href="{% url 'logout' %}">
What are the analogous tags for registering an account and resetting the password?
Currently I am doing this which feels rather ugly:
<href="/accounts/password/reset/">
<href="/accounts/register/">
What's the proper way to do this please?
Password Reset
As mentioned in the docs, it should be:
<a href="{% url 'password_reset' %}">
Registration
This is usually supported by third party libraries. It seems you might be using the django-registration default backend, in which case it is:
<a href="{% url 'registration_register' %}">
AFAIK you need to install django-registration to get these.
Then you can use:
{% url 'auth_password_reset' %}
{% url 'registration_register' %}
EDIT
For password reset you can use password_reset. See the docs.