How to hide 0 notification count in django-notifications-hq - django

I ham trying to hide the notification count when it's 0 in django-notifications-hq
I have tried the below method but it is not updating regularly and displaying the number correctly.
{% live_notify_badge as nc %}
{% if nc > 0|add:0 %}
<span class="badge-notifications badge badge-pill badge-danger" style="float:right;margin-bottom:-3px;margin-top: -2px !important; margin-left: 10px !important; font-size: 0.6rem;">
{% live_notify_badge %}</span>
{% endif %}

nc is not the number of notifications. It generates some HTML that will make Javascript calls to fetch the number of notifications.
You can obtain the number of unread notifications in the template with:
{{ user.notifications.unread.count }}
So we can check if an unread notification exists, and use this to render the {% live_notify_badge %}:
{% if user.notifications.unread.exists %}
<span class="badge-notifications badge badge-pill badge-danger" style="float:right;margin-bottom:-3px;margin-top: -2px !important; margin-left:10px !important; font-size: 0.6rem;">
{% live_notify_badge %}
</span>
{% endif %}
Note however that this will be rendered at server side, so that means that when the user fetches the page, and there are no notifications, it will not display the badge. If however later there are notifictions these will not be rendered.

Related

Insert multi selected option text into pg using django

For instance, I used Django to pull certain unique number data from my database.
Then I created dropdown with search using select2. And used jQuery for search functionality. With this I could select multiple unique number at a time and with output printed in textarea field.
my question is, how to insert multiple selected options in Postgrel using Django.
I need help in backend as i m newbie in django
I tried to served everywhere but I could see only php guide.
this is my frontend code
enter code here
Search Models for CId
{% csrf_token %}
{% for x in searchmodels %}
{{ x.cid }}
{% endfor %}
You can add multiple CID and click Add
-->
<br>
<div class="resultbox">
<h2>Selected CIDs</h2>
<textarea name="" id="cidresult" cols="70" rows="15"></textarea><br><br>
<input type="button" id="addcid" value="Submit" onclick="searchresult();">
</div>
</form>
</tr>
<style>
.resultbox{
position: fixed;
left: 30%;
top: 10px;
width: 550px;
background-color: antiquewhite;
padding-left: 30px;
padding-right: 10px;
}
</style>
Now here is my jQuery code
$(document).ready(function()
{
$('#selectedcid').select2({
placeholder:'Search CID...',
closeOnSelect:false
});
$('#selectedcid').on('change',function()
{
var resultdisplay=$('#selectedcid option:selected').text();
$('#cidresult').val(resultdisplay).join(',');
})
});
this is how it looks nowenter image description here

base.css is not get imported into the page?

I am trying to override the class generated by page-down app and the django-crispy-forms . But the indentation that is expected through overrideing class is not working.
base.html(initially)
...
<link rel='stylesheet' href='{% static "css/base.css" %}'>
<style>
{% block style %}{% endblock style %}
</style>
{% block head_extra %}{% endblock head_extra %}
...
base.css
h1 {
color: #777777;
}
post_forms.html
{%extends "base.html" %}
{% load crispy_forms_tags %}
{% block head_extra %}
{{form.media}}
{% endblock head_extra %}
...
By using the inspect feature in chrome I can spot the class that causes the indentation
<div class="wmd-panel">
</div>
The code CSS given below is automatically generated one
.wmd-panel {
margin-left: 25%;
margin-right: 25%;
width: 50%;
min-width: 500px;
}
But after making changes to css/base.css , there is no class named wmd-panel from the file base.css in the styles tab of the chrome. And the changes made are not reflected in the webpage.
base.css
h1 {
color: #777777;
}
.wmd-panel{
margin-right: 0px !important;
margin-left: 0px !important;
}
This is what expected in chrome inspect styles tab
.wmd-panel {
margin-left: 0%;
margin-right: 0%;
}
This above class is from basic.css
You should clean the chrome cache and check again. Sometimes it just does not update the code and you are just viewing the old CSS even you have made changes to it.
Settings>Advanced>ClearBrowsingData>
and delete all Cached images and files
Do you have {% load static %}? Sometimes you just forget that. And often you miss STATIC_URL = '/static/' in your settings.

Moving the header in django-admin

i am new to django, and i put the image in my admin header, but the name is blocking it, so i was wondering how to move the name a bit to the middle of the page. Simply adding the spaces in front of the name doesnt work, because for some reason it ignores all the extra spaces.
{% block extrastyle %}
<style type='text/css'>
#header {
background-color: #0c407a;
}
#branding {
height:94px;
background-color: #0c407a;
background: white url('/static/logo2.jpg') no-repeat ;
}
</style>
{% endblock %}
{% block branding %}
<h1 id="myid">{{ _(' MyName') }}</h1>
{% endblock %}
maybe you can try with adding some CSS code:
#myid{
margin-left: 30px;
}

Pinax : Add new tab to the basic_projects application : Confused by documentation

Just cloned the basic_project and trying to customize by following this - http://pinaxproject.com/docs/0.7/tabs/#ref-tabs
Created a new app "myapp", added the new tab to the right_nav and edited the "site_tabs.css" as well.
However when I click on the tab, although the page does change to myapp, the background color of the tab doesn't change.
This line in the documentation - Create a myapps/base.html template that all pages under that tab will extend. Make sure it defines a block body_class with content myapp is confusing me.
What is that "body_class" with content myapp ?, is it some "div" with the class having "{% block body_class%}" ?
My code of the myapp page is pretty simple right now -
{% extends "site_base.html" %}
{% load i18n %}
{% load ifsetting_tag %}
{% block head_title %}
{% trans "Custom App page" %}
{% endblock %}
< div class="myapp">
< h1 >
{% trans "Custom App page" %}</h1>
{% if user.is_authenticated %}
< p >You are signed in !!</p>
{% else %}
< p >You are NOT signed in !!</p>
{% endif %}
< /div >
The site_base.css is as follows -
body.profile #tab_profile a,
body.myapp #tab_myapp a,
body.notices #tab_notices a
{
color: #000; /* selected tab text colour */
}
body.profile #tab_profile,
body.myapp #tab_myapp,
body.notices #tab_notices
{
margin: 0; /* to compensate for border */
padding: 5px 0 5px;
background-color: #DEF; /* selected tab colour */
border-left: 1px solid #000; /* tab border */
border-top: 1px solid #000; /* tab border */
border-right: 1px solid #000; /* tab border */
}
Any pointers would be great. Thanks.
Just define a block named body_class inside your site_base.html with the content myapp like this:
{% block body_class %}myapp{% endblock %}
This will override the block defined in base.html.
<body class="{% block body_class %}{% endblock %}">
It is probably used to by CSS to make your tab active. See the example at the bottom of the documentation.
body.profile #tab_profile a,
body.blogs #tab_blogs a,
body.bookmarks #tab_bookmarks a
{
color: #000; /* selected tab text colour */
}
Now, if you're opening pages that use your template, the body tag looks like this:
<body class="myapp">
Therefore the CSS selector above can match for your tab.
body.myapp #tab_myapp a { // active

How to format form fields for django comments?

I am using a form which is generated for me by django. I'm using this as a comment form after a post in my blog.
Currently it renders fine but it's not nicely aligned.
This is what I have.
This is what I'd like.
Thanks
edit: This is the result when I user {{ form.as_table }}
I know it's a little late, but for everybody else you can try this
<table>
{{ form.as_table }}
</table>
It fixes the annoying formatting problem, and looks decent.
Posting my solution hoping it'll help someone else oneday.
I knew that you'd style the fields with css but wasn't sure how to assign the classes to each item. But if you take a look at the default template provided you'll notice that the error class is assigned to a field using an if statement within a foreach loop that automatically generates each field within your form.
i.e.
{% for field in form %}
< p{% if field.errors %}
class="error"
{% endif %}
{{ field.label_tag }}<'/' p>
{% endfor %}
So I added onto this functionality.
< p{% if field.errors %}
class="error"
{% endif %}
{% ifequal field.name "honeypot" %}
id="hide"
{% else %}
id="left"
{% endifequal %}>
{{ field.label_tag }}<'/' p>
my css being
#hide{
display:none;
}
#left{
width: 200px;
text-align: left;
}
#right{
width: 300px;
text-align: left;
}
Now that you can set your classes you can easily setup your classes or id within your css file. This is for the comments.
If you're using a {{ form.as_p }} or {{ form.as_table }} to generate your form then you just set a general form class within your css to style it.
i.e.
form {
width: 350px;
padding: 20px;
border: 1px solid #270644;
}
Have a look at Customizing the form template. This is one possible solution.
Maybe you can simply use CSS to style your form and render the form as you like. (i.e as_table()).
Using the default {% render_comment_form for app.model %} will generate:
<p>
<label for="id_name">Name</label>
<input id="id_name" type="text" name="name" maxlength="50">
</p>
<p>
<label for="id_email">Email address</label>
<input type="text" name="email" id="id_email">
</p>
... etc
Therefore, you can target label in your CSS stylesheet using:
label {
width: 15%;
}
There are multiple approaches as detailed in this post, however, I found that sometimes widget attributes may not work the way you wanted.
Still, the best and easy way is to use CSS:
Render your page that contains the form fields and do some inspection (right click>inspect or F12 on Chrome for example) to know what html tags your form generates when rendered. Then you can write your CSS easily.
You have input and textarea fields in your form so your CSS will be:
input, textarea{
width:350px;
}
Don't forget to call for your CSS file at the top of your html template:
<link rel="stylesheet" type="text/css" href="{% static 'styles/form.css' %}">
Here is a snapshot of what I have for my own form: