how to avoid referer checking in django? - django

I have to web page using django.
In html, I assign external image link to img src tag.
but, 403 forbidden error and not show image.
when I paste image external link to browser address, the image show.
I think.. it is referer checking. so I use ReferrerKiller.js in Changing the http referer in javascript.
first image is shown. but other is not.
I check network using chrome developer tool.
other image canceled. I don't know that.
I want to listen any idea about this problem.. Referer checking and why show only first image? other not?
below home.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="{% static "js/ReferrerKiller.js" %}"></script>
<title>island</title>
</head>
<body>
<h1> nanpa </h1>
<br/>
{% for entrySummary in entrySummaryList %}
title :
{{ entrySummary.entry_title }}
{{ entrySummary.entry_pub_date }} <br/>
description : {{ entrySummary.entry_description }} <br/>
image : <span id="image_kill_referrer"></span>
<!-- <img src= ("{{ entrySummary.entry_representaion_img }}"/> -->
<script>
document.getElementById('image_kill_referrer').innerHTML = ReferrerKiller.imageHtml("{{
entrySummary.entry_representaion_img }}");
</script>
{% endfor %}
</body>
</html>

document.getElementById() returns only one item (the first item, because all images in you code have same id). Use another methods like document.getElementsByClassName.
Try following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="{% static "js/ReferrerKiller.js" %}"></script>
<title>island</title>
</head>
<body>
<h1> nanpa </h1>
<br/>
{% for entrySummary in entrySummaryList %}
title :
{{ entrySummary.entry_title }}
{{ entrySummary.entry_pub_date }} <br/>
description : {{ entrySummary.entry_description }} <br/>
image : <span class="image_kill_referrer"></span>
{% endfor %}
<script>
var i, images = document.getElementsByClassName('image_kill_referrer');
var urls = [
{% for entrySummary in entrySummaryList %}
"{{ entrySummary.entry_representaion_img }}",
{% endfor %}
"DUMMY"
];
for (i = 0; i < images.length; i++) {
images[i].innerHTML = ReferrerKiller.imageHtml(urls[i]);
}
</script>
</body>
</html>
Or, use different id for each image:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="{% static "js/ReferrerKiller.js" %}"></script>
<title>island</title>
</head>
<body>
<h1> nanpa </h1>
<br/>
{% for entrySummary in entrySummaryList %}
title :
{{ entrySummary.entry_title }}
{{ entrySummary.entry_pub_date }} <br/>
description : {{ entrySummary.entry_description }} <br/>
image : <span id="image_kill_referrer{{ forloop.counter }}"></span>
<script>
document.getElementById('image_kill_referrer{{ forloop.counter }}').innerHTML = ReferrerKiller.imageHtml("{{
entrySummary.entry_representaion_img }}");
</script>
{% endfor %}
</body>
</html>

Related

Django Flash Message shows up without implementing it in template

i want to show up my Flash Messages in my template. It works, but i cant tell the template where to show or how to show it. It appears all the time in the same place and the same style.
This is my view:
def exportBefunde(request):
(...)
messages.info(request, "Befund exportiert")
return redirect("reporting")
This is my main.html template
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Das neueste kompilierte und minimierte CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optionales Theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Das neueste kompilierte und minimierte JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="{% static 'index.css' %}">
<title>Message Problem - it drives me crazy</title>
</head>
<body>
{% include 'navbar.html' %}
{% block content %}
<!-- Content from other templates -->
{% endblock content %}
{% if messages %}
{% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}
{% endif %}
</body>
</html>
I want to show my template underneath my content. But i appears above it. Even if i remove this peace of code:
{% if messages %}
{% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}
{% endif %}
the message shows up above the code. The styling is always the same. Django ignores my Code in my template. Do anyone has a solution for this issue?

Inheriting the Django admin header

I have my own HTML page "stat.html" which shows some graphs, this is hyperlinked from a custom index page that I made using this method answer3.
My current admin/custom_admin.html page looks like this
{% extends "admin/index.html" %}
{% block content %}
{{block.super}}
<div class="module">
<table style="width:100%">
<caption>
<a class="section">Statistics </a>
</caption>
<tbody>
<th scope="row">
Stats Page
</th>
</tbody>
</table>
</div>
{% endblock %}
The link /myapp/statpage is link to a static html page, it is showing two google charts in it, something like this
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Stats</title>
<script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script type = "text/javascript" src =
"https://www.gstatic.com/charts/loader.js"> </script>
<script type = "text/javascript"> google.charts.load('current',
{packages: ['corechart']}); </script>
<script src="{% static "v_stats.js" %}"></script>
</head>
<body>
<div id="container" style="width:80%; height:35vw; margin:"auto";"></div>
<div id="container1" style="width:80%; height:35vw; margin:"auto";"></div>
</body>
</html>
How do I just inherit the Django admin header in this stat.html page and have my own title, some links to static js pages and some content in it?
from myproject.admin import admin_site
admin.site.site_header = 'My administration'
You may now wish to use this elsewhere in your site, by inserting it as context into your templates.

rendered script display inside body template django

Sorry for my bad english.
I use
{% ishout.js %}
inside head to render scripts file in django template but they display as string
"<script type="text/javascript" src="http://localhost:5500/socket.io/socket.io.js"></script>
<script type="text/javascript" src="http://localhost:5500/client/ishout.client.js"></script>"
and inside body not inside head.
And other scripts after that are displayed in body too.
I searched and find out by encoding but i cannot find how to fix it.
UPDATE
This is views.py file
#login_required
def home(request):
users = User.objects.exclude(id=request.user.id)
v = RequestContext(request, {'users':users})
# return render(request, 'home.html', {'users':users})
return render_to_response('home.html', v)
This is home.html file
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
{% load drealtimetags %}
{% ishout_js %}
</head>
<body>
<h1>Dashboard</h1>
{% for user in users %}
{{ user.first_name }}Alert (Hello)
{% empty %}
<b>No user found</b>
{% endfor %}
</body>
</html>
UPDATE 2:
This is page source
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Home</title>
<script type="text/javascript" src="http://localhost:5500/socket.io/socket.io.js"></script>
<script type="text/javascript" src="http://localhost:5500/client/ishout.client.js"></script>
<script>
ishout.on('alertchannel', function(data){
alert(data.msg);
});
ishout.init();
</script>
</head>
<body>
<h1>Dashboard</h1>
phucAlert (Hello)
Alert (Hello)
</body>
</html>
And when I inspect
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
<body>
<script type="text/javascript" src="http://localhost:5500/socket.io/socket.io.js"></script>
<script type="text/javascript" src="http://localhost:5500/client/ishout.client.js"></script>
<script>
ishout.on('alertchannel', function(data){
alert(data.msg);
});
ishout.init();
</script>
<h1>Dashboard</h1>
phucAlert (Hello)
Alert (Hello)
</body>
Snapshot
It's because the tag is returning HTML that is getting escaped, so the browser is rendering it as text inside the body instead of as real HTML in the <head>.
Turn off autoescape around the tag:
{% autoescape off %}
{% ishout_js %}
{% endautoescape %}
Since you are including each of those js files, and you want it rendered and interpreted as html script tags, try making it a template file _ishout_includes.html instead. You don't need to change the contents of the file at all.
From there, use the {%include%} tag. It would look like
{% include _ishout_includes.html%}

Why is my Django template inheritance not working?

I'm working with Django1.8.
I can't overtwrite the version_tag block in my base.html file. It always appears the default content.
This is a simplified version of my base.html:
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="{% static 'myApp/img/favicon.png' %}" rel="icon" type="image/png" />
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% static 'myApp/lib/css/bootstrap.min.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'myApp/lib/css/themes/bootstrap.min.css' %}" />
<script type="text/javascript" src="{% static 'myApp/lib/js/jquery.min.js' %}"></script>
</script>
</head>
<body>
<div id="main">
{% block menu %}
{% include "myApp/menu.html" %}
{% endblock menu %}
<div id="container" class="container-fluid">
{% block content %}
{% endblock content %}
</div>
</div>
{% block version_tag %}Default Version{% endblock version_tag %}
</body>
</html>
On the same folder I have the version_tag.html file:
{% extends 'myApp/base.html' %}
{% block version_tag %}
V. 1.2.3
{% endblock %}
What am I missing or doing wrong? Why does it always appear Default Version instead of V. 1.2.3?
There are only two possible causes I can think of:
Your view is still trying to render base.html instead of your inherited html template.
Simply change the view to call the correct one (version_tag.html)
The template you're really trying to display is still inheriting from base.html instead of version_tag.html

Trying to create a DetailView page in Django

I used Django to create a blog and i want to display a single post on page when i click on a post title and no matter of the used method i cannot display anything on the page.
My Views:
class One_Per_Page(DetailView):
model = AboutMe
objects = AboutMe(id)
def oneperpage(request):
entry = One_Per_Page.objects.get_queryset(pk=AboutMe.id)
#entries = super(One_Per_Page).get_queryset()
return render_to_response('blog/aboutme_detail.html', {'AboutMe': entry})
this is the URL i use:
url(r'^(?P<pk>\d+)$', One_Per_Page.as_view(), name='oneperpage
this is the html page that must display a single post when the title of it is cliked:
<!DOCTYPE html>
<html lang="en">
<head>
<title>EBA</title>
<meta charset="utf-8"/>
<link rel="stylesheet" href="/static/css/styles.css" type="text/css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body class="body">
<header class="mainHead">
<img src="/static/img/mainlogo.png" %} width="700" height="144" %}>
<nav>
<ul>
<li><a href='/'>Home</a></li>
<li><a href='/blog/'>Blog</a></li>
<li><a href='{{ STATIC_URL }}/latestnews/'>News</a></li>
<li><a href='{{ STATIC_URL }}/archive/'>Archive</a></li>
</ul>
</nav>
</header>
{% block content %}
<div>
<article>
<h4>{{ entry.titleMe }}</h4>
<p class="postInfo">
on {{ entry.dateMe }}
</p>
<div class="typicalArticle">
{{ entry.contentMe|safe|linebreaks }}
</div>
</article>
</div>
{% endblock %}
<footer class="mainFooter">
<p> copyright © 2015</p>
</footer>
</body>
</html>
Can anyone help me to do this right?
I dont know what error you are getting but try this, actually you dont need to do much:
url(r'^stuff/(?P<pk>\d+)/', One_Per_Page.as_view(
template_name='detail.html'), name="detail")
and
class One_Per_Page(DetailView):
model = AboutMe
and in template, you have automatically object instance you can access like this:
{{ object.name }}
{{ object.biography }}