Fixing Hg Mistakes

Removing Pushes from Other Branches

I often create a branch by cloning. I do more involved or experimental things in the clone. While the parent branch is available to make small minor updates. As code in the branch progresses, I do commits. And every once in a while, by accident, I do a commit and push. This puts the experimental code in the main branch before its ready. To remove the pushed change set, cd into main and do:

hg strip -r REV

					

Django HTTP 404 Not Found Error

So you’ve got everything working on the Django dev server. Almost everything is working on the staging server… except one URL that sometimes fails with an HTTP 404 Not Found error. What is going on here?

More details; it happens to be a Django form page. The page loads with a problem. The problem occurs after you submit the form. The URL that causes the 404 error is the URL of the form. What is going on here?

In my case, the form_valid() was doing a lot of processing of the form data and the server was timing out. Maybe part of the reason I did not catch this earlier is my dev server is much faster than the Digital Ocean droplet I was using.

Python Dates and Times Cheatsheet

Python (2.7) datetimes are a little frustrating because there are many ways to do each manipulation and there are many applicable 3rd party libraries. Below are code snippets that I think show the best solution. While I appreciate the concept of minimizing dependencies, there are some 3rd party libraries that are so useful, that I just include them as requirements on all my projects.

Aware UTC Now


import datetime

import pytz

now = pytz.utc.localize(datetime.datetime.utcnow())

Select2 vs Chosen

Select2 and Chosen are both great javascript widgets for enhancing the HTML select tag. They have a lot of similarity and a few differences. How to pick? These just a few issues I have run into. I am using Select2 V4 and Chosen 1.1.

Form Tabbing

A lot of users prefer keyboard to a mouse. Both widgets allow that. However, Chosen handles tab better. With Chosen, tab completes the entry and moves to the next form field. With Select2, I could not get tab to move to the next field. Select2 messes with the tabindex property. I am guessing that is part of the problem.

Tagging

This is the reason I used Select2 instead of Chosen. Tagging allows the user to input something that is not already in the select widget.

 

 

Django and Memcached: Set Value Not Working

I had a strange memcached bug while using Django. Sometime cache.set(key, value) would save the value. Other times it would not. No warnings or errors. Just not saved.

It turns out the item I was trying to save was near the item size limit of memcached. The default is 1 MB (see the -I flag). Sometimes the item would be less than the max and set would work. Other times it would exceed the limit and set would just stop working.

The man page recommends not exceeding the default. So I solved the problem by saving the item in two parts.

I should also mention that Django file-based cache is pretty easy to use and does not have this limitation. Since all my Django sites run on SSD drives, file-based cache is almost as fast as memcached.

 

Selenium and Firefox Problems

I just did a software update on my Ubuntu 14.04 development machine. That update included updating Firefox to 47.0. This caused my tests to fail with the message:

Traceback (most recent call last):
 File "/home/chuck/sqdb/django_fuller_calendar/django_fuller_calendar/tests/tests_selenium.py", line 23, in setUp
 self.browser = webdriver.Firefox()
 File "/home/chuck/.virtualenvs/dfc/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 81, in __init__
 self.binary, timeout)
 File "/home/chuck/.virtualenvs/dfc/local/lib/python2.7/site-packages/selenium/webdriver/firefox/extension_connection.py", line 51, in __init__
 self.binary.launch_browser(self.profile, timeout=timeout)
 File "/home/chuck/.virtualenvs/dfc/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
 self._wait_until_connectable(timeout=timeout)
 File "/home/chuck/.virtualenvs/dfc/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 98, in _wait_until_connectable
 raise WebDriverException("The browser appears to have exited "
WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

I am using selenium 2.53.5.

After extensive googling, the most useful page was: https://github.com/SeleniumHQ/selenium/issues/2110

On that page, this comment was most useful:

I strongly suggest people move over to Marionette

Following the instructions on that page, I was able to get the python demo code on the bottom of that page to work. I needed to change the name of the file to “wires” as mentioned in the text. Putting the file in /usr/bin made it available everywhere I need it.

I should mention that my selenium tests are now failing because I am using the command:

action.move_to_element_with_offset

Earlier today, I tried switching to the Chromium browser. It failed on the same command.

Rolling Back Firefox

The solution above did not work for me because I could not find a work-around for the “move_to” commands. So I rolled back Firefox as a temporary solution. I used the instructions found here: http://askubuntu.com/questions/661186/how-to-install-previous-firefox-version  I was able to find the version I wanted here: https://sourceforge.net/projects/ubuntuzilla/files/mozilla/apt/pool/main/f/firefox-mozilla-build/

 

Django Error: ‘Form’ object has no attribute ‘get’

Suppose you get this error while trying to render a page that has a form:

‘Form’ object has no attribute ‘get’

Did you use super correctly??? Or did you make the dreaded “cut and paste” error:

class FormWithError(forms.ModelForm):
    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(FormWithError, self).__init__(self, *args, **kwargs)


 class FormWithOutError(forms.ModelForm):
    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(FormWithError, self).__init__(*args, **kwargs)

Note the extra “self” in the super().__init__.

Feel free to slap yourself in the head.