I don't know how do write javascript for Django?
Please help me
The following javascript code is correct?
This code is write in the HTML:
base.html:
{% load staticfiles %}
index.html
{% extends "base.html" %}
<html>
<head>
</head>
<body>
<script type="text/javascript">
$(document).ready(function()
{
$("#sampleTable").tablesorter();
);
</script>
<div class="import">
<table id="sampleTable" class="tablesorter">
<thead>
<tr>
<th class="{sorter:'metadata'}" style="width:100px">name</th>
<th class="{sorter:'metadata'}" style="width:260px">company</th>
</tr>
</thead>
</table>
{% if memo.count > 0 %}
{% for user in memo %}
<div><h3>
<table id="sampleTable1" class="tablesorter">
<tbody>
<td style=" border-bottom:1px solid #0099cc; text-align:center;">{{ user.user_name }}</td>
<td style=" border-bottom:1px solid #0099cc; text-align:center;">{{ user.company }}</td>
</tbody>
</table>
</div>
</body>
</html>
Django has nothing to do with Javascript by default. It just renders the templates and returns HTML. You write Javascript for Django, like you'd write anywhere else.
And whether that particular code is correct, Run it. If it runs then it's correct, if not, you have some error. But chances are they wont be related to Django.
BTW, your code is incomplete. You haven't closed the if and for blocks in the template. Also, if you're extending base.html, then your HTML should be inside a block that you defined in base.html. Please read the documentation before writing codes.
Related
I'm trying to add a background color to even rows in a table in HTML.
I'm using xhtml2pdf to convert the html to pdf to serve with a django backend.
The styles on the even rows don't seem to be working
<html>
<head>
<style type="text/css">
tr:nth-child(even) {
background-color: blue;
}
</style>
</head>
<body>
<main>
<table>
<thead>
<tr>
<th>Heading/th>
<th>Heading</th>
<th>Heading</th>
</tr>
</thead>
<tbody>
{% for result in data.results %}
<tr>
<td>{{result}}</td>
<td>{{result}}</td>
<td>{{result}}</td>
</tr>
{% endfor %}
</table>
</main>
</body>
</html>
I have a problem with rendering templates in Django. Blocks don't seem to appear in the DOM position they were defined in. Here is my code:
I'm using a base template (base.html):
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Partwell Transactional Email</title>
<style>
...
</style>
</head>
<body>
<span class="preheader">Partwell email</span>
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="body">
<tr>
<td> </td>
<td class="container">
<div class="content">
<!-- START MAIN CONTENT AREA -->
<div class="content-main">
{% block content %} {% endblock content %}
{% block content_extended %} {% endblock content_extended %}
</div>
<!-- END MAIN CONTENT AREA -->
{% block unsubscribe %} {% endblock unsubscribe %}
<!-- START FOOTER -->
<div class="footer">
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="content-block">
{% block behalf %} {% endblock behalf %}
</td>
</tr>
<tr>
<br />
<span class="apple-link">********Amtsgericht Charlottenburg *********</span>
<span class="apple-link"> *****</span>
<br />
</tr>
</table>
</div>
<!-- END FOOTER -->
</div>
</td>
<td> </td>
</tr>
</table>
</body>
</html>
And a template that extends the base template. This template is also a base template for all transactional emails (tenant-base.html):
{% extends "base.html" %}
{% block behalf %}
This email was sent on behalf of {{ tenant.name }}.
{% endblock behalf %}
Ultimately, I'm using the tenant-base.html to create the final template:
{% extends "tenant-base.html" %}
{% block content %}
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="main">
<tr>
[...]
</tr>
</table>
{% endblock content %}
The final result looks like this:
You can see that the behalf block was rendered lower than it was originally defined. It is not the last line defined in the template, yet it moves down in the dom. I'm seeing this behavior with other blocks too. I would love to know why this happens and how to avoid it.
This is not a Django template problem but happens because one of your <tr>...</tr> has no <td>...</td> inside.
If I change your footer to this it works:
<div class="footer">
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="content-block">
{% block behalf %} {% endblock behalf %}
</td>
</tr>
<tr>
<td>
<br />
<span class="apple-link">********Amtsgericht Charlottenburg *********</span>
<span class="apple-link"> *****</span>
<br />
</td>
</tr>
</table>
</div>
(Please note the added <td>...</td> in the second table row.)
I am working on a project right now, particularly on the CRUD phase. I am also new on the Django framework, but I knew some basic stuff about it. I did everything stated on the manual but it's weird because I got this kind of error.
I am using Django 2.2. Here's my code, by the way.
base.html
<!-- base.html -->
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>{% block title %}Title{% endblock %}: CAO-SMS Admin</title>
{% load static %}
<link rel = "stylesheet" href = "{% static 'fisher_prof/style.css' %}"/>
</head>
<body>
{% block body %}Body{% endblock %}
</body>
</html>
list.html
<!-- Displays the whole database (fisher) -->
{% extends 'fisher_prof/base.html' %}
{% block title %} Fisher's Information {% endblock %}
{% block body %}
<br>
<center>
<h1>Fisher's Database</h1>
<table class = "table table-striped table-bordered table-sm">
<thead class = "thead-dark">
<tr>
<th>ID  </th>
<th>Fisher's Last Name</th>
<th>Fisher's First Name</th>
<th>Fisher's Middle Name</th>
<th>Mobile Number</th>
<th>Actions</th>
</tr>
</thead>
{% for fisher in fishers %}
<tr>
<td>{{fisher.last_name}}</td>
<td>{{fisher.first_name}}</td>
<td>{{fisher.mid_name}}</td>
<td>{{fisher.cell_num}}</td>
</tr>
{% endfor %}
</table>
<br> <br>
</center>
{% endblock %}
Then when I run it, Django tells me this:
Invalid block tag on line 41: 'endblock', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
Is there something wrong with my code? I really need help.
I have a datatable in Django that functions fine when hardcoded, but when I add my Django template tags it breaks. The inspect on the page says: Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined in jquery.datatables.min.js
This only happens when I have more than one user in the table, or try to add a column in the table with django. Since I will be using multiple datatables in my project, I need to figure out what I'm doing wrong. The datatable JS code I'm using comes from a template, and I'm not sure if the error is in the template code or in my Django template code. So please excuse the long code blocks.
employees.html (django template):
<div class="card-body collapse in">
<div class="card-block card-dashboard">
<button id="addRow" class="btn btn-primary mb-2 js-create-employee"><i class="ft-plus"></i> Add New Employee</button>
<table class="table table-striped table-bordered zero-configuration">
<thead>
<tr>
<th>Name</th>
<th>Username</th>
<th>Roles</th>
<th>Email</th>
<th>Mobile Contact</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for profile in user_profile_list %}
<tr>
{% if not profile.user.is_superuser %}
<td>{{ profile.user.get_full_name }}</td>
<td>{{ profile.user.username }}</td>
<td>
{% for g in profile.user.groups.all %}
<div class="tag tag-default">{{ g.name|split:'_'|title }}</div>
{% endfor %}
</td>
<td>{{ profile.user.email }}</td>
<td>{{ profile.mobile_phone }}</td>
<td><i class="fa fa-pencil"></i> <a href="#" alt="Assign"><i class="fa fa-link"></i><a/> <a href="#" alt="delete"><i class="fa fa-times"></i><a/></td>
{% endif %}
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th>Name</th>
<th>Username</th>
<th>Roles</th>
<th>Email</th>
<th>Mobile Contact</th>
<th>Actions</th>
</tr>
</tfoot>
</table>
Datatable instantiation:
$(document).ready(function() {
/****************************************
* js of zero configuration *
****************************************/
$('.zero-configuration').DataTable();
});
Jquery.datatables.min.js and dataTables.bootstrap4.min.js are also used, but those come stock from bootstrap 4. I'm not going to add them here unless needed, and they are minified anyway.
This issues occurs only when data is not available for the table or tags.
You have conditions in template using django templatestag So the number of every <td> element in your table that is a child of a <tr> element doesn't match the number of <th> elements that are a child of the element.
Please make sure you have same number of <td> tag in <tbody> tag.
EDIT:
Maybe {% if not profile.user.is_superuser %} this condition creating this issue. If user is superuser then no <td> tag will create that will not match same number of <th> in <thead>
Have a homepage there every page with a form works fine.
Added a form to my start page and in my local server it works fine.
But when I add it to the production server it does not work.
It say's that CSRF- token is missing or incorret.
But I have added the token, and it works all the other pages.
What is that I'm missing... ?
View
#login_required
def start(request) :
libs = Library.objects.all();
header = Header('Start');
studies = None;
source = None;
if request.method == 'POST' :
if 'Show_studie' in request.POST:
studies = Study.objects.all;
if 'Show_source' in request.POST:
source = Source.objects.all;
dctArgs = {
'library_list': libs,
'styles_dir': conf.styles_path,
'header': header.html,
'studies_list':studies,
'source_list':source,
'images_dir': conf.images_path,
};
return render_to_response('start.html', dctArgs, RequestContext(request));
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE></TITLE>
<link rel="stylesheet" type="text/css" href="{{styles_dir}}/common.css" />
<link rel="stylesheet" type="text/css" href="{{styles_dir}}/header.css" />
<link rel="stylesheet" type="text/css" href="{{styles_dir}}/Headerstyles.css" />
</HEAD>
<BODY>
{{header|safe}}
<h1></h1>
<table id="doc_tbl" class="data" cellspacing=0>
<tr>
<th>Name</th>
<th>Documents</th>
<th>Export</th>
</tr>
{% for library in library_list %}
<tr>
<td>{{library.name}}</td>
<td>{{library.source_set.all|length}}</td>
<td>Andra till Jesper Export</td>
</tr>
{% endfor %}
</table>
<h3>Messages</h3>
{{messages}}
</br>
<form id="form1" name="form1" method="post" action="/start/" enctype="multipart/form-data">
{% csrf_token %}
<fieldset style="width:300px; margin-left:5px;">
<legend> Show all </legend>
<input type="checkbox" name="Show_studie" value="Show Studie"> Studie
<input type="checkbox" name="Show_source" value="Show Source"> Source
</br>
</br>
<input type="submit" value="Show All">
</fieldset>
{% if studies_list %}
<h3> Studies </h3>
<table class="table1">
<thead>
<tr>
<th></th>
<th scope="col1" >Name</th>
<th scope="col1" >Added by</th>
</tr>
</thead>
{% for study in studies_list %}
<tbody>
<tr>
<td>
<img class="icon" src="{{images_dir}}/edit-icon.png"/>
<img onclick="javascript:return confirmDelete_name('Are you sure? The study and any associated information will be deleted.', {{study.id}}, 'delete_study');" class="icon" src="{{images_dir}}/delete-icon.png"/>
</td>
<td>{{study.name}}</td>
<td>{{study.metadata_added_by_user.first_name}} {{study.metadata_added_by_user.last_name}}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{% if source_list %}
<h3> Source </h3>
<table class="table1">
<thead>
<tr>
<th></th>
<th scope="col1" >Name</th>
<th scope="col1" >Added by</th>
</tr>
</thead>
{% for source in source_list %}
<tbody>
<tr>
<td>
<img class="icon" src="{{images_dir}}/edit-icon.png"/>
<img onclick="javascript:return confirmDelete_name('Are you sure? The study and any associated information will be deleted.', {{study.id}}, 'delete_study');" class="icon" src="{{images_dir}}/delete-icon.png"/>
</td>
<td>{{source.name}}</td>
<td>{{source.metadata_added_by_user.first_name}} {{source.metadata_added_by_user.last_name}}</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</form>
</BODY>
</HTML>
Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF token missing or incorrect.
In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's
CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
• The view function uses RequestContext for the template, instead of Context.
• In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
• If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views
that use the csrf_token template tag, as well as those that accept the POST data.
You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.
Settings
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
On the page that working I get this message:
<django.contrib.messages.storage.fallback.FallbackStorage object at 0x03B7A270>
Try wrapping your view in the #requires_csrf_token decorator, like so:
from django.views.decorators.csrf import requires_csrf_token
#requires_csrf_token
#login_required
def start(request):
...
You can use the csrf_exempt decorator to disable CSRF protection for a particular view.
from django.views.decorators.csrf import csrf_exempt
#csrf_exempt
def someview():
......
I know Its not what you want but you can try this if you want :)