LiveReload. Best $12 Bucks Spent Ever.

nuff said

nuff said

I’ve been working on angular.js using yeoman for the last couple of weeks. One thing I really liked about it is the live reload functionality. Essentially, any changes made in the DOM or CSS gets updated immediately. It’s a really really powerful feature for web developers. It’s also one of those features you never knew you needed until you’ve used it.

I got so addicted to it, I went hunting around for an app that is able to do the same for my django projects. Enter LiveReload which is quite possibly the best invention since sliced bread. The setup is super trivial. Add my django project to the folder list, install a chrome extension and I’m done. Now, all changes in the DOM or CSS gets updated immediately when I’m working on Django. Best SGD $12 bucks spent on an app ever.

Advertisements

Some Lessons Learnt from Messing with Django and AngularJS

django-logo-negative

square

  1. Key take-away: If you’re thinking of sneaking angular.js as you would any other js file into your ‘static’ folder, you gonna have a bad time. It starts to get really messy when you try to mix and match angular’s url routing mechanism vs. django’s url. The same applies to the templates. I imagine this messiness will always be present if you try to mix a client-side MVC framework with a server-side one.  The best way is to separate angular out from your django project entirely and use the services to communicate with your django application. This means you will need something like django-tastypie or django-rest-framework setup on a separate app. The rest of the points are really more learnings about angular so read on if you’re interested.
  2. You will be confused between the differences of a service and a factory. I’m still confused.
  3. Angular kinda requires you to have a solid understanding of Javascript to be fully effective. You could get away with knowing less Python and still be effective in Django in my opinion.
  4. You can only use one ng-view per view
  5. If your controllers seem to be firing twice, it’s probably because you defined an ng-controller in your html as well as in your route.
  6. If you skipped reading the conceptual overview, it’s a critical document that will help you wrap your head around the angular concepts so don’t.
  7. Angular disables form submissions without an action attribute. If you’re using django-crispy-forms (there’s not reason not to. it’s an excellent form library) to render your form, simply add self.helper.form_action = ‘#’ to fix this.
  8. If you’re wondering why your url end point is getting called twice, its probably because Django is redirecting the url with a trailing slash to a url that does. To fix this, simply escape the backslash like: /custom_api/get_nearest_hotels/:eventId\\/. There is a gotcha to this. It only works in Chrome and Safari. If you’re calling the endpoints via a service factory, Firefox will decide that it’s a good idea to remove the trailing slash from the URL and encode the backslash that was escaping the forward slash to %5C. The result is to use $http. See related angular issue.
  9. Loading templates are a bit of a hassle. There are a few ways to solve this. This guy’s method.
  10. Tricks and tips
    1. http://gpiot.com/angularjs-useful-tips-to-get-started/
    2. http://deansofer.com/posts/view/14/AngularJs-Tips-and-Tricks-UPDATED
  11. Angular and SEO
  12. Get the correct angular folder structure from angular seed
  13. Check out angular-ui
  14. AngularJS in 60 mins
  15. If you see your page flash with angular template tags and then reload again to show the actual content, you need to apply ng-cloak.
  16. $scope.$watch will fire on page load to initialize the watcher.
  17. Always have a ‘.’ in your models. e.g ng-model=user.name and not ng-model=username
  18. $scope.$apply is essentially the way you get the rest of your javascript to sync with angular’s javascript.
  19. Controllers should not contain presentation logic — that belongs in Directives. Controllers strictly should be used for business logic.
  20. If you’re using a moment.js (awesome library) for datetime manipulation, don’t forget that the angular format filters only operate on javascript date objects. So you’ll have to append a .toDate() to cast your moment object into a javascript date.

Shell Plus, iPython or Shell Prompt Looks Screwed Up

If your prompt ever looks like this:

Python 2.7.3 (default, Oct 22 2012, 06:12:32)
Type "copyright", "credits" or "license" for more information.

IPython 0.13 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
[0;34mIn [[1;34m1[0;34m]: [0m

To fix, simply run: easy_install -a readline

Django Crispy Forms Add HTML5 Data Attributes to Button

Add HTML5 data attributes directly to the button like this:

self.helper.layout = Layout(
                'first_name',
                'last_name',
                'email',
                FormActions(
                    Submit('add', 'Add', css_class="btn-primary"),
                    Button('cancel', 'Cancel', css_class="btn-danger",
                        data_dismiss="modal", aria_hidden="true"),
                    css_class="form-actions form-custom"
                )
            )

instead of this:

HTML('<input type="button" name="cancel" value="Cancel" \
                            id="button-id-cancel" \
                 data-dismiss="modal" aria-hidden="true">'),

13 SEO Tips for Django

This is mostly an on-site specific guide
  1. Use the redirects app to manage url changes Django documentation
  2. Use post save signals to handle slug/url changes in your models Link
  3. Use sitemap The sitemap framework
  4. Use slugfield Model field reference
  5. Use cached template loaders to reduce page load times The Django template language: For Python programmers
  6. Use a css/js compressor to reduce page load times django_compressor
  7. Use django-robots to manage your robots file django-robots
  8. If you’re rolling an e-commerce site, don’t reinvent the wheel, just go with The Best Django CMS. It’s the best code and seo friendly cms framework for django
  9. Manage object level meta data with django-seo
  10. Not django specific but submit your site to google webmasters Webmasters – Google. For bing: Bing – Webmaster Tools
  11. Not django specific again but load your page using Make the Web Faster and make all the recommended changes
  12. Crawl your site content to find broken links using Xenu or Integrity for Mac
  13. To generate image sitemaps, use Django Image Sitemaps

Other Stuff

django.core.exceptions.ImproperlyConfigured: Could not import user-defined GEOMETRY_BACKEND “geos”.

You’re installing GeoDjango and you meet this error “django.core.exceptions.ImproperlyConfigured: Could not import user-defined GEOMETRY_BACKEND “geos” while trying to runserver (or any other command) in your project. Check the following in order of severity:

Have you included this in your .bash_profile (or equivalent)

export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/opt/local/lib/postgresql91

Where 91 is the version of postgres you’re using.

If that doesn’t work, I would try reinstalling all the packages listed here: https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#macports

Make sure you’re getting the right versions for each package and that postgis is installing to the right postgres!