Trouble getting django-chosen ChosenSelect widget to work - django

I'm trying to set up a ModelForm in Django that will use the django-chosen ChosenSelect widget for one of the fields. I have installed django-chosen and this is the code I have:
class TestForm(ModelForm):
class Meta:
model = Test
widgets = {
'field': chosenwidgets.ChosenSelect(),
}
However, the specifying the widgets has no effect and the form is outputted the same regardless of whether I define it. Any suggestions?

Are you including the form media in your template?
The example template below assumes that your base template has blocks extrastyle and extrahead where you include CSS and scripts respectively, and that you have included jquery 1.4+ in your base template.
# my_template.html
{% extends "base.html" %}
{% block extrastyle %}
{{ block.super }}
{{ form.media.css }}
{% endblock %}
{% block extrahead %}
{{ block.super }}
{{ form.media.js }}
{% endblock %}
{% block content %}
<form action="." method="post">
<table>
{{ form }}
</table>
<p><input type="submit" value="Update" />
</form>
{% endblock %}

Here is my solution with using chosen.js directly. I installed it with bower and then included it in my admin class Media:
class MyModelAdmin(LockableAdmin, admin.ModelAdmin):
form = MyModelForm # has phase_id field
class Media:
js = ('components/chosen_v1.1.0/chosen.jquery.min.js', 'admin_tools/js/chosen_admin.js')
css = {'all': ('components/chosen_v1.1.0/chosen.min.css', 'admin_tools/css/chosen_admin.css')}
The additional chosen_admin.js and chosen_admin.css files contain my init script and css for making the select look more like django admin. Here are the contents:
chosen_admin.js:
/* Create own jquery namespace */
var django = {
"jQuery": django.jQuery.noConflict(true)
};
var jQuery = django.jQuery;
var $=jQuery;
$( document ).ready(function() {
$('#id_phase_id').chosen();
});
chosen_admin.css (optional):
a.chosen-single {
box-shadow: none;
border-radius: 0px;
background: white;
background-image: none;
}
a.chosen-single span{
line-height: 18px !important;
}
.vTextField{
width: 24em;
}
a.chosen-single, .chosen-container.chosen-container-single, .chosen-container-single.chosen-single{
position: absolute;
box-shadow: none !important;
border-radius: 0px !important;
background: white !important;
background-image: none !important;
font-size: 11px !important;
height: 18px !important;
padding-left: 2px !important;
}
.chosen-container-single .chosen-single div b {
background-position: 0px 0px;
}
I got some insights here and made corrections as needed. Also see chosen.js. Hope this helps :)

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

Django IntegrityError at /doctor-add-notes NOT NULL constraint failed:

I am a newbie.
Enhancing a project For the current user, there are different members, for each member I want to add Notes with the current time & date. so I am using Django in-built user model.
for example
User1
Member1 Notes Date & Time
Member2 Notes Date & Time
Member3 Notes Date & Time
**User2**
Member1 Notes Date & Time
Member2 Notes Date & Time
Member3 Notes Date & Time
for now just trying to add user1, member1 Notes & Date but after submitting the form getting an error. how to correct that,
IntegrityError at /doctor-add-notes
NOT NULL constraint failed: hospital_notes.user_id
Request Method: POST
Request URL: http://127.0.0.1:8000/doctor-add-notes
Django Version: 3.0.7
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: hospital_notes.user_id
Exception Location: C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 396
Python Executable: C:\Users\user1\AppData\Local\Programs\Python\Python38\python.exe
Python Version: 3.8.3
Python Path:
['C:\\Users\\user1\\PycharmProjects\\Project\\hospitalmanagement',
'C:\\Users\\user1\\PycharmProjects\\Project\\hospitalmanagement',
'C:\\Users\\user1\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',
'C:\\Users\\user1\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',
'C:\\Users\\user1\\AppData\\Local\\Programs\\Python\\Python38\\lib',
'C:\\Users\\user1\\AppData\\Local\\Programs\\Python\\Python38',
'C:\\Users\\user1\\AppData\\Roaming\\Python\\Python38\\site-packages',
'C:\\Users\\user1\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages']
Server time: Thu, 31 Dec 2020 03:37:51 +0000
Here is part of relevant code
**Models**
class Notes(models.Model):
user=models.ForeignKey(User,on_delete=models.CASCADE)
report = models.TextField(max_length=500)
NoteDate = models.DateField(auto_now=True)
**View:**
def doctor_add_notes_view(request):
appointmentForm=forms.PatientNotesForm()
mydict={'appointmentForm':appointmentForm,}
print(mydict)
if request.method=='POST':
appointmentForm=forms.PatientNotesForm(request.POST)
if appointmentForm.is_valid():
appointment=appointmentForm.save(commit=False)
appointment.save()
return HttpResponseRedirect('doctor_add_notes')
else:
return render(request, 'hospital/doctor_view_patient.html',{'alert_flag': True})
return render(request,'hospital/doctor_add_notes.html',context=mydict)
**Forms:**
class PatientNotesForm(forms.ModelForm):
class Meta:
model=models.Notes
fields=['report']
**doctor_add_notes.html**
{% extends 'hospital/doctor_base.html' %}
{% load widget_tweaks %}
{% block content %}
<head>
<style media="screen">
a:link {
text-decoration: none;
}
.note {
text-align: center;
height: 80px;
background: -webkit-linear-gradient(left, #0072ff, #8811c5);
color: #fff;
font-weight: bold;
line-height: 80px;
}
.form-content {
padding: 5%;
border: 1px solid #ced4da;
margin-bottom: 2%;
}
.form-control {
border-radius: 1.5rem;
}
.btnSubmit {
border: none;
border-radius: 1.5rem;
padding: 1%;
width: 20%;
cursor: pointer;
background: #0062cc;
color: #fff;
}
.menu {
top: 50px;
}
</style>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<br><br>
<form method="post">
{% csrf_token %}
<div class="container register-form">
<div class="form">
<div class="note">
<p>Patient Notes Details</p>
</div>
<div class="form-content">
<div class="row">
<div class="col-md-12">
<div class="form-group">
{% render_field appointmentForm.report class="form-control" placeholder="Description" %}
</div>
</div>
</div>
<button type="submit" class="btnSubmit">Book</button>
</div>
</div>
</div>
</form>
{% endblock content %}

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