-
-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Doesn't work with jQuery 3 #197
Comments
my ChainedManyToManyField get no selects, i don't know why, and how to fix it..... #my models.py from django.db import models
from smart_selects.db_fields import ChainedManyToManyField
class size(models.Model):
size_name = models.CharField(max_length=255)
def __str__(self):
return self.size_name
class quality(models.Model):
quality_name = models.CharField(max_length=255)
quality_size = models.ManyToManyField(size)
def __str__(self):
return self.quality_name
class PL(models.Model):
PL_name = models.CharField(max_length=255)
PL_quality = models.ForeignKey(quality,related_name='q1')
PL_size = ChainedManyToManyField(
quality,
horizontal=True,
chained_field='PL_quality',
chained_model_field='quality_size',
) #my forms.py from django import forms
from django.forms import ModelForm
from .models import quality,PL
class PL_form(ModelForm):
class Meta:
model = quality
fields = "__all__" # ('quality','size')
class PL_form2(ModelForm):
class Meta:
model = PL
fields = "__all__" #my views.py from django.shortcuts import render
from aa.forms import PL_form,PL_form2
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def home(request):
form = PL_form()
form2 = PL_form2
return render(request,'home.html',{'form':form,'form2':form2}) #my home.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/static/jquery-3.2.0.js"></script>
<script src="/static/chainedfk.js"></script>
<script src="/static/chainedm2m.js"></script>
<script>$(document).ready(function () {
$("#ibutton").click(function () {
$.post('/',{a:1},function () {
alert($("#id_size").val())
})
})
})
</script>
</head>
<body>
{{ csrf_token }}
{{ form }}
<button id="ibutton">确认</button>
{{ form2 }}
</body>
</html> #my web get: <label for="id_PL_size">PL size:</label>
<select id="id_PL_size" class="chained selectfilter" multiple="multiple" data-field-name="PL size" name="PL_size" required=""> </select>
<script type="text/javascript">
(function($) {
var chainfield = "#id_PL_quality";
var url = "/chaining/filter/aa/quality/quality_size/aa/PL/PL_size";
var id = "#id_PL_size";
var value = "";
var auto_choose = false;
// Use $(window).load to call function after SelectBox and SelectFilter2
$(window).load(function() {
chainedm2m.init(chainfield, url, id, value, auto_choose);
});
})(jQuery || django.jQuery);
</script> Edit (by Blag): Formatted code blocks with GFM. |
Please format your code with GitHub Flavored Markdown by using fenced blocks:
gets rendered as: def this_python_function():
# Get rendered properly
return awesome |
You seem to be trying to render the PL_form2 class: def home(request):
form = PL_form()
form2 = PL_form2 # <-- HERE
return render(request,'home.html',{'form':form,'form2':form2}) when you should be trying to render the constructed form object: def home(request):
form = PL_form()
form2 = PL_form2() # <-- Note the () at the end
return render(request,'home.html',{'form':form,'form2':form2}) See if that fixes it. |
sorry,i am first time to use git, thanks your help!!! |
No worries! 😄 Did my suggestion fix your issue? |
Foreignkey is works,but ManyToManyField is not work...it's the js cause this question? <tr><th><label for="id_PL_name">PL name:</label></th><td><input id="id_PL_name" maxlength="255" name="PL_name" type="text" required /></td></tr>
<tr><th><label for="id_PL_quality">PL quality:</label></th><td><select id="id_PL_quality" name="PL_quality" required>
<option value="" selected="selected">---------</option>
<option value="1">pvc</option>
</select></td></tr>
<tr><th><label for="id_PL_size">PL size:</label></th><td><select multiple="multiple" class="chained selectfilter" data-field-name="PL size" id="id_PL_size" name="PL_size" required>
</select>
<script type="text/javascript">
(function($) {
var chainfield = "#id_PL_quality";
var url = "/chaining/filter/apppppp/quality/quality_size/apppppp/PL/PL_size";
var id = "#id_PL_size";
var value = "";
var auto_choose = false;
// Use $(window).load to call function after SelectBox and SelectFilter2
$(window).load(function() {
chainedm2m.init(chainfield, url, id, value, auto_choose);
});
})(jQuery || django.jQuery);
</script>
</td></tr> |
What is actually happening? What do you expect to happen? Are you loading jQuery twice on the page? Are there any errors in your browser console? |
1、when i access /chaining/filter/apppppp/quality/quality_size/apppppp/PL/PL_size, it show didn't match any of these. |
here is my html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/static/jquery-3.2.0.js"></script>
<script src="/static/chainedfk.js"></script>
<script src="/static/chainedm2m.js"></script>
<script>$(document).ready(function () {
$("#ibutton").click(function () {
$.post('/',{a:1},function () {
alert($("#id_size").val())
})
})
})
</script>
</head>
<body>
{{ csrf_token }}
{{ form }}
<button id="ibutton">确认</button>
{{ form2 }}
</body>
</html> |
|
ohhhh, you remind me. |
Are there any errors in your browser console? |
no errors in my browser console! |
i think that models.py or ChaineManyToManyField something is wrong? |
from django.db import models
from smart_selects.db_fields import ChainedManyToManyField
class size(models.Model):
size_name = models.CharField(max_length=255)
def __str__(self):
return self.size_name
class quality(models.Model):
quality_name = models.CharField(max_length=255)
quality_size = models.ManyToManyField(size)
def __str__(self):
return self.quality_name
class PL(models.Model):
PL_name = models.CharField(max_length=255)
PL_quality = models.ForeignKey(quality,related_name='q1')
PL_size = ChainedManyToManyField(
quality,
horizontal=True,
chained_field='PL_quality',
chained_model_field='quality_size',
)
|
Try taking jQuery and the second form out of your template: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
{# <script src="/static/jquery-3.2.0.js"></script> #}
<script src="/static/chainedfk.js"></script>
<script src="/static/chainedm2m.js"></script>
<script>$(document).ready(function () {
$("#ibutton").click(function () {
$.post('/',{a:1},function () {
alert($("#id_size").val())
})
})
})
</script>
</head>
<body>
{{ csrf_token }}
{{ form }}
<button id="ibutton">确认</button>
{# And comment out this #}
{# {{ form2 }} #}
</body>
</html> django-smart-selects may load its own jQuery (depending on your settings), and including two forms may overwrite that. |
i change my html. |
chainedfk.js and chainedm2m.js can GET. |
oh i get something !! ReferenceError: jQuery is not defined |
Working with jQuery is a little weird because that error can be caused if jQuery is never loaded, or if it is loaded multiple times. View the generated HTML and verify that jQuery is being loaded exactly once, and tweak your settings until that happens. Once you do that, then check your browser console for errors, check the AJAX request and response, etc. |
thanks , i cheak it. |
i know why ,something is wrong in the jquery-3.2.0.js . |
Oh, django-smart-selects may not be compatible with jQuery 3+. You're the first user to be using jQuery 3, so if you fix the issue and create a pull request for me I'll merge it in. |
i want to fix issue ,but i am new bird. i am not sure i can fix it. 😢 |
No worries, either somebody else will fix it or you can learn. I'll keep this issue open until it's fixed. |
Until it's fixed, can you use jQuery 2? |
ok!!!,i just start to learn django,jquery2 or 3 is same for me,thanks your help!! |
This seems to be fixed. Please reopen if necessary. |
All versions of django-smart-selects prior to version 1.2.8 are vulnerable to an XSS attack as detailed in issue 171. As a result, all previous versions have been removed from PyPI to prevent users from installing insecure versions. All users are urged to upgrade as soon as possible.
Checklist
master
branch of django-smart-selects.models.py
,forms.py
, andviews.py
with problems.Steps to reproduce
Expected behavior
Actual behavior
The text was updated successfully, but these errors were encountered: