Adjust or change hidden value in form before submit with JQuery does not work

So here’s something rather odd, if you have a form:

<form id="process" action="/form-submit-url" method="post">
  <input id="myinput" name="mydata" type="hidden" />
  <button type="submit">Submit</button>
</form>

And you try and populate the hidden mydata input using jquery before the form submits:

 $(document).ready(function() {
   $('#process').submit(function(event) {
     $('#myinput').val('hello world')
   });
 });

It won’t work. You can’t do any of these sort of things either:

 $('input[name=mydata]').val('hello world');
 $('#myinput').attr('value', 'hello world');
 etc 

You have to do:

$('input[name=mydata]').val('hello world'); 

Quite strange. See http://stackoverflow.com/questions/2979772/set-value-of-hidden-field-in-a-form-using-jquerys-val-doesnt-work

The non-jquery method works fine!

document.getElementById("mydata").value = 'hello world';

Venetia, by Georgette Heyer

This is one of the most highly rated Heyer books on Goodreads, but I just don’t understand why.

The heroine is an ingenue with a little bit of spirit to her, and generally quite a nice and intelligent character. She is sexually molested (kissed and held down) by the hero on their first encounter, in something that feels very much like it could easily have turned into a rape scene. She’s outraged, but seems to go straight to being friends with the hero without thinking much about what he did. There’s quite a lot of moralising about how it’s in men’s natures to cheat and they can’t help themselves and boohoo poor men they are always so misused and noble, whereas the woman who cheats is such a pathetic slut.

Everything he had done since he had seen himself as a laughing-stock (and she neither knew nor cared to what depths he might have sunk) she perceived to be part of a pattern made inevitable by a wanton’s betrayal.

And all for a little, plump, black-eyed slut, older than himself, whose marriage-ring and noble degree hid the soul of a courtesan!

That passage is from the point of view of the heroine as she thinks about the hero. This is the second time I’ve tried to read Venetia, and the first time I couldn’t stand it and had to put it away because of that very passage as far as I remember. I suppose it must be an historically accurate representation of that society’s prevalent opinions, but I just don’t like it.

“Do you imagine he would be faithful to you?”
“I don’t know,” said Venetia. “I think he will always love me. You see, we are such dear friends.”

I feel like the book tries to be a bit more realistic and nuanced than your standard Heyer, with Venetia having to come to terms with the hero’s nature and having to accept it. I think it’s the fact that it takes itself a bit more seriously which really puts me off and makes it harder to dismiss the slut shaming and constant soothing of men’s egos which occurs throughout the book. It’s infuriating.

I like Georgette Heyer’s heroines because generally they are sparky, don’t take any bullshit (especially the Grand Sophy!), are strong-willed and generally fun to spend book-time with. Venetia wasn’t, and the situation in the book was horrible.

Validation examples for handsontable

In my current project, I have users who want to regularly copy and paste stuff from excel and save it. At first I tried an elaborate system where they would download a spreadsheet template, paste their data into it, upload it and get back a new spreadsheet with validation errors. It started getting quite complicated, and I was very happy to find the handsontable library (an awesome lightweight spreadsheet library) to use instead. The problem is that there aren’t many examples on how to add validation on the web. So I hope this helps some other poor soul like me.

First thing is to stop trying to use handsontable’s methods via jquery. I spent ages trying to get it working and getting terribly confused because examples on the internet were all using jquery but the documentation on the site was all just pure js. Definitely easiest to go the pure js way, don’t forget you can include pure js code in your jquery $(document).ready(function(){}) – it will work just fine. JQuery is just javascript after all!

So, here is an example on a few different types of validation methods:

 HTML:
<div id="exampleGrid" class="dataTable"></div>
<button id="submitData" type="button" class="btn btn-primary">Upload</button>

JS: (remember to include jquery for your bits and bobs, as well as handsontable's js and css)
var myData = [
 ["cat", '10/11/2015', ''],
 ["invalidvalue", '10/11/2015', ''],
 ["dog", '10/11/2015', ''],
 ["", '10/11/2015', '']
];

// Empty validator
emptyValidator = function(value, callback) {
 if (!value || 0 === value.length) {
 console.log('false');
 callback(false);
 } else {
 console.log('true');
 callback(true);
 }
};

// Handsontable options
hotOptions = {
 colHeaders: ['autocomplete','date', 'string'],
 minSpareRows: 0, // This must be 0 or else it tries to add a blank row as soon as you remove it for validation
 rowHeaders: true,
 contextMenu: true,
 colWidths: [240, 100, 100],
 columns: [
 { // Note, whenever you put "type: something", handsontable does its own validation and if you add a "validator: yourfunction" it seems to go haywire.
 type: 'autocomplete',
 source: ['cat', 'dog'],
 strict: true
 },
 { // It is more obvious with the date that some kind of validation is going on and it will get confused with your own custom validator. I don't know how you would add custom date validation, I haven't had to do that yet thank goodness
 type: 'date',
 dateFormat: 'DD/MM/YYYY',
 correctFormat: true,
 defaultDate: '01/01/2000'
 },
 { // Here is the empty column with your validator forcing text in there
 validator: emptyValidator
 }
 ],
 data: myData,
};

// Instantiate
var ht = new Handsontable(document.getElementById('exampleGrid'), hotOptions);

// Trigger more validation with the click
$('#submitData').click(function() {
 // Get the data in the cells
 var myTableData = ht.getData();

 // If the last row is empty, remove it before validation
 if(myTableData.length > 1 && ht.isEmptyRow(myTableData.length - 1)) {
 // Remove the last row if it's empty
 ht.alter('remove_row', parseInt(myTableData.length - 1), keepEmptyRows = false);
 }

 // Validate the cells and submit the form via ajax or whatever
 ht.validateCells(function(result, obj) {
 if(result == true) {
 console.log('submitted');
 }
 else {
 console.log('bad form data')
 }
 });
});

And here is a link to a jsFiddle: https://jsfiddle.net/sysb6wvd/2/

Django-allauth and modelforms

It took me a ridiculously long and frustrating time to work out how to do this, so I’m documenting what I’ve done in the hope that it helps someone else. If you want to extend the Django user model (to create a user profile, as described in the docs https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-the-existing-user-model) to create a user profile, and to still use Django-allauth you might as well give up on the documentation: it’s not going to help you. But what you need to do is fairly simple:

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    phone = models.CharField(max_length=100)

forms.py

class SignupForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('first_name', 'last_name', 'phone', 'type')

    # A custom method required to work with django-allauth, see https://stackoverflow.com/questions/12303478/how-to-customize-user-profile-when-using-django-allauth
    def signup(self, request, user):
        # Save your user
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

        # Save your profile
        profile = Profile()
        profile.user = user
        profile.phone = self.cleaned_data['phone']
        profile.type = self.cleaned_data['type']
        profile.save()

settings.py

# Required by django-allauth to extend the sign up form to include profile data
ACCOUNT_SIGNUP_FORM_CLASS = 'core.forms.SignupForm'

I’m not sure why I thought that django-allauth would take care of saving user profiles when it very explicitly says it doesn’t do anything apart from authentication. I suppose we should count ourselves lucky that it’s easy to extend the form to include additional profile fields.

Django multi select multiple choice model field

To me, it doesn’t really make sense to store static data in a database – especially if there are only 5 or 6 errr datum you are wanting to store. Django allows you to use a Charfield with choices, i.e.

WIND = 'W'
SOLAR = 'S'
ENERGY_TYPE_CHOICES = (
    (WIND, 'Wind turbine'),
    (SOLAR, 'Solar panels')
)
energy_type = models.CharField(max_length=1, choices=ENERGY_TYPE_CHOICES, default=WIND)

But unfortunately you can’t select multiple choices, so in this case it wouldn’t be possible to associate both wind & solar with one of my models. It is suggested that developers store this info in a separate table and associate the choices with models using a manytomanyfield. Perhaps this makes sense if you want to associate the field options with multiple models, but it really seems overkill if you just want to have multiple choices for a single model.

Strangely there don’t seem to be many other people who have felt this to be too much (I mean, going by google anyway). As I have frequently found with Django, a few people have discussed it on stackoverflow and there are a million ways of doing the same thing with no clear indiciation as to which is most widely used and most sensible. Well, there are 3 different modules/alternatives:

  • Multiselectfield: https://github.com/goinnn/django-multiselectfield
  • Multiple_select_field: https://gist.github.com/kottenator/9a50e4207cff15c03f8e
  • Select_multiple_field: https://github.com/kelvinwong-ca/django-select-multiple-field

This is ridiculous. Django’s “do-it-yourself” attitude is refreshing after coming from PHP and Drupal, but why isn’t something this ubiquitous integrated into Django itself?  This is something many projects are going to want. Or do I have it wrong and is it better to have a separate db table for like 2 or 3 fields?

Django error: TemplateResponseMixin requires either a definition of ‘template_name’ or an implementation of ‘get_template_names()’

This is such a bizarre error message and I have spent hours trying to figure out what on earth I’ve been doing wrong. This is my situation: I have several models for which I am creating generic views for (ListView, DetailView, CreateView, UpdateView, DeleteView). These mostly contain postgis geographical data that I am storing as polygonfields and so on. To display these nicely in my forms I want to use the django-leaflet LeafletWidget. To do THIS I have to create a modelform. So for example my files look like:

models.py:

class Project(models.Model):
    current_name = models.CharField(max_length=50)
    location = models.PolygonField()
    objects = models.GeoManager()

views.py:

class ProjectCreate(CreateView):
    model = models.Project
    template_name_suffix = '_create_form'
    form_class = forms.ProjectCreateForm

forms.py:

class ProjectCreateForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = ('current_name', 'location')
        widgets = {'location': LeafletWidget()}

urls.py:

urlpatterns = [
    url(r'^project/create$', views.ProjectCreate.as_view(), name='project_create'),
]

now, if you leave out the model = x bit in the createview (which i think is perfectly reasonable as you have to specify in the modelform in the forms.py what model you want to use, then you get this error:

TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'

Absolutely infuriating. I can’t believe how many hours it took for me to track this down. All because I left it yesterday and didn’t put the model = x bit back into the code when I removed it to see what happened. I also can’t believe I actually forgot about it though – senility is clearly on the horizon.

It is actually in the docs though – https://docs.djangoproject.com/en/1.8/topics/class-based-views/generic-editing/ scroll down to the first note inset. Lesson learned: always read the docs rather than googling for stackoverflow answers!

Terry Pratchett

You know, I can’t believe both Terry Pratchett and Diana Wynne Jones are gone now. Two people whose books I would look forward to and read on a regular basis. I hope we see their like again, but I just haven’t found any authors who I enjoy in the same way.

A lot of people I know, most people actually, didn’t really like Pratchett’s writing. And I can sort of understand, the books are a bit jarringly zany, but if you’re the kind of person who can quite happily be carried off on a madcap adventure then you will enjoy them.

He certainly wasn’t the most subtle writer in the world, but I loved his force and his vitriol and his complete disdain for the crap people can come up with. It’s purer and raw-er than most of the other authors I like, but I don’t enjoy it any less at all.

And really the books were very clever, they skewered everything that was wrong with the world and stuck it on a cork board for everyone to laugh at. And he had so much colour and diversity in his books, and so many different kinds of people and different ways of making fun (and I mean that in the literal sense, as in, he manufactured fun and happiness and wonderfulness) of them.

Well I feel quite sad now.

WordPress, you suck for websites with lots of pages and no posts

My recent experiences with wordpress have made me realise that it really is a terrible solution for a website with a lot of pages (say more than 30) and no posts. It’s great at blogging though. But say you have lots of pages, you’re gonna want a menu to show those pages right? And you’re probably going to want 2 menus, one for the ‘about/contact/partners/faq’ to-do-with-the-company content, and one for the primary content. If your primary content custom menu is likely to be long (as it is in my case, at around 130 pages+), then the default menu system is cumbersome and dragging things around between levels is a complete nightmare.

And there is no solution. You can display page hierarchies instead, but this sucks because it’s useful to think about your site in terms of the menu structure, and to do half of your menus using custom menus and half of your menus using page hierarchy is shit. It’s also impossible to get your menus to automatically sync with your page hierarchy. I miss drupal. Here’s hoping this post annoys someone enough for them to post and tell me an easy way of doing what I want (just a simple menu which is easy to manage!).