Thursday, January 31, 2013

VIM Arrow Keys

I wasn't planning on posting today but I ran the updates for my Ubuntu system yesterday and now when using the arrow keys in VIM I get letters.  Really annoying.  Doing a quick search turned up that the repo has a better version of VIM than the packaged version and fixes the issue.  So if you're on Ubuntu and the arrow keys are writing A B C or D then run the following command.

sudo apt-get install vim

Wednesday, January 30, 2013

Goals for myself and this Blog

In the previous article I wrote about installing PyDev and how getting familiar with Eclipse was one of my goals.  I want to make more of an outline on what I want to accomplish over the next couple of months.  As I brainstormed I came up with the following (in no particular order at this time).

  • Version Control - Git
  • Create a GitHub account
  • Write Python code using version control and GitHub
  • Write another Django application
  • Install Django on my Ubuntu Desktop and use that for Django development.
  • Update my homepage, ronaldsnyder.net.  Create a dynamic interactive resume.
  • Improve HTML and CSS skills
  • Improve Javascript and jQuery
  • Write a Web App using Twitter
  • Use jQuery Masonry
  • Checkout Twitter Bootstrap
  • create a To Do and progress list on RonaldSnyder.net to keep track of goals and progress.

There is a bunch to do but version control jumps out to me as the first thing that needs to be done.   The other projects can benefit from using Git and GitHub and start gaining experience using it.

I haven't written any Python in a while, the Django tutorial didn't feel like I was writing code.  It was more about learning how to navigate Django and the basics of what it can do.  The next step would be to write some Python code.  The next project will be processing data on a website and write the interesting parts to either a text file, csv or sqllite database.

Next would be to install Django on my desktop and find another tutorial to walk through.  That should help get more CSS and HTML experience too.  After that is completed I would like to update ronaldsnyder.net with a Django application for my resume or CV.   I would like one resume with my current skills and a future one with the skills I am going to learn (no intention of misleading anyone, it is for the purpose of motivating myself)  So here is my list of things to accomplish in February:

  1. Version control using Git and GitHub
  2. In Python, parse a website and pull out the information you need.
  3. Install Django on Ubuntu and work through another tutorial 
  4. Update ronaldsnyder.net resume page.  
There is more in my brainstorm but those four are good for the next four weeks.  This will be an ongoing process of returning to the brainstorm, adding more tasks and creating a plan.

Installing Eclipse/PyDev on Ubuntu 12.04

I was sent to Groovy on Grails training this week for work and we are using the Eclipse powered Groovy Grails Tool Suite.   So it had me thinking, I have been using gEdit for writing all of my Python code up until now.  It has been one of my goals to get familiar with Eclipse, is there something similar for Python?  I looked and PyDev fits the bill.  

Here is a step by step on how I installed Eclipse/PyDev on Ubuntu.  The first thing I did was install Eclipse through the Ubuntu software center, it comes right up with a search.  After it finished downloading and installing I tried launching it and immediately had an error.  This error told me to look at the log file:

/home/rsnyder/.eclipse/org.eclipse.platform_3.7.0_155965261/configuration/1359586660119.log

That log file contained:

!ENTRY org.eclipse.osgi 4 0 2013-01-30 17:57:40.822
!MESSAGE Application error
!STACK 1
java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons:
        no swt-gtk-3740 in java.library.path
        no swt-gtk in java.library.path
        Can't load library: /home/rsnyder/.swt/lib/linux/x86_64/libswt-gtk-3740.so
        Can't load library: /home/rsnyder/.swt/lib/linux/x86_64/libswt-gtk.so

It appears the SWT libraries moved and Eclipse couldn't find them.  To fix this you can make a symbolic link to the new location:

ln -s /usr/lib/jni/libswt-* ~/.swt/lib/linux/x86/
ln -s /usr/lib/jni/libswt-* ~/.swt/lib/linux/x86_64/
After making the symbolic link Eclipse now launches fine. Now onto installing PyDev.

1. Launch Eclipse.  Open the Help tab and click Install New Software
2. Click the Add button on the top right.
3. Enter the name and url:

Name:  PyDev 
URL:   http://pydev.org/updates



4. After entering the name and url hit OK. On the next screen only check the PyDev box and then click next.



5.  Click Next again, accept the license agreement and click Finish.  I had to accept the certificates for PyDev and then restart Eclipse.  

6.  Once Eclipse starts back up Go to Windows tab, Preferences and Open up the PyDev menu.  Choose the Python Interpreter like below.


7.  Click Auto Config and another window will open for the SYSTEMPATH.  Choose Select All and then OK.


8.  You will be taken back to the previous screen and now click Apply and then OK.    You should now have PyDev working on Eclipse. 

Thursday, January 24, 2013

404 on your Home Index.html?

Since I setup django I have had a 404 error on the index page.  Not good.  I did attempt to fix it one evening and didn't have any luck.  At that point I decided I would finish the tutorial and then try to fix it.  Well, I have finished the tutorial and now have a better understanding of some of the basics.  I was able to get my index.html back up and working in rather quickly.

Here are the steps I took:

Navigated to myTemplates and created a directory within it named main.  I copied over the index.html page into the new main directory (home/rsnyder/mytemplates/main/index.html)

I created a views.py (~/first_django/myfirst/myfirst) in the same directory as the base urls.py (the first urls.py looked at).  That file contained:


from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.template import Context, loader
#quick fix to get index back up and going
def index(request):
    template = loader.get_template('main/index.html')
    #context isn't used but it appears I need to pass something
    context = Context({'name': ['Ronald', 'D', 'Snyder']})
    return HttpResponse(template.render(context))

In the same directory there were two changes that were need in the urls.py.  You need add this the imports:


import views


And this as the first search in urlpatterns:

url(r'^$', views.index, name='index'),

That was it.  I bet that there is a better and more conventional way to do this.  This was the way I came up with having only a week of Django experience and my plan is to make the main page more dynamic and interesting soon.




Wednesday, January 23, 2013

Django Tutorial on Hostgator part 4

I went through part 4 of the Django tutorial last night.  The mix up between the dev and 1.4 version caught me again.  It caused multiple problems and what I felt should have taken an hour took most of the evening.  The problem was found after creating results.html and testing the application.  After hitting the vote button this error comes up:  Reverse for 'polls.views.results' with arguments '(1L,)' and keyword arguments '{}' not found.


It appeared that a lot of people had the problem [1] [2] [3].  I tried the suggestions in the posts and tweaked code to no avail.   After spending an hour I decided that I had to go back to the start of the tutorial and check everything. What I found was that in polls/models.py file the Choice class had a variable change names between the 1.4 version and the dev version.  The variable was called choice in 1.4 and choices_text in dev.  So in the mix up of using different documentation at different times both were used in the code.   I decided since the database was built using choices_text that I would go through and change anywhere I used the invalid choice attribute and update it to choices_text.


I ended up changing it 2 times:


in detail.html from:
<label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br />

to:
<label for="choice{{ forloop.counter }}">{{ choice.choices_text }}</label><br />


in results.html from:
<li>{{ choice.choice}} -- {{ choice.votes }} vote {{ choice.votes|pluralize }}</li>

to:
<li>{{ choice.choices_text}} -- {{ choice.votes }} vote {{ choice.votes|pluralize }}</li>

I also had an error in polls/view.py in the vote function which I changed to:
return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))

After that I had a working poll that I could vote on!  I plan on sharing all of the code from this on github if anyone is interested.  I will share that link when it is available.

Monday, January 21, 2013

Django Tutorial on Hostgator part 3

It has taken me a little longer to get back to this than I wanted. I was able to complete part 3 of the official django tutorial with only one slight problem. I was getting an error of NoReverseMatch at /polls/

So it was easily fixed with adding this line to the top of my index.html file:

{% load url from future %}

When I was looking back at this solution for the blog post I noticed another peculiar thing.  I have been working off from two different tutorials on the official django site.  DOH!!!!!  In one blog post I was looking at /dev/intro and the other was 1.4/intro.  I should have been paying more attention.  I believe I should have been using the 1.4 version all along.  To be sure I open python and type:

import django
django.VERSION



As suspected, I should have been using the 1.4 documentation.  Everything I did the last few days worked except for the NoReverseMatch error I received which was easily fixed.  No harm done, this time.


Wednesday, January 16, 2013

Django Setup on Hostgator part 2

I really thought setting this up would be quicker and easier than it is.  It seems every step of the way I am having issues.  I was going to sit down and continue working on the tutorial Monday but ended up finding the main page of my site was getting a 404 error.  I must have broke it when I enabled the fast CGI and not had a view created for the main page.  I ended up searching for a way to fix that with a static page and didn't come up with a working solution.  I renamed index.fcgi so I had some content up.  On Tuesday I decided that I would venture on with the tutorial and that with more understanding I would be able to fix it.  Nothing lost, the purpose of the site is learning.  Please don't think I am complaining because I am not.  I believe I am learning more from having to do some of this troubleshooting.

I am basically working my way through page two now and everything seems straight forward but..... I keep having an issue where my changes are not showing up in the browser.  I edit the admin.py file and I am not seeing the expected changes on the web.  I don't think I have to update the database but I run python manage.py syncdb just in case.  Same thing.  I do some more searching for a solution and come up empty.  I refresh the page using CTRL-F5 and the changes appear.  Cool, just cached.  I can deal with that.  I make some more changes and have the same problem.  I do a CTRL-F5 and the content stays the same.  Why aren't my settings.py or admin.py changes not taking effect? UGH!

I ended up doing more searching and found a Stackoverflow Post that had the same issue.  It was suggested to run the command:  touch index.fcgi

That didn't work but the the following command has every time so far:

killall -USR1 index.fcgi

I think the process either hangs up or isn't seeing the updates and needs to be restarted.  Hopefully this solution will keep working, it was getting frustrating working through the tutorial and having to wait for the changes to take effect.


The rest of tutorial 3 went well.  The one minor issues was I copied over the templates from the Django install into a directory called mytemplates when it should have been in mytemplates/admin.  I will continue on with the rest of the tutorial and post any other issues.





Sunday, January 13, 2013

Django Setup on Hostgator Part 1

It is Sunday and I am excited to get django installed and running ronaldsnyder.net hosted by hostgator.   I have zero experience with Django or any web frame work.  I have found an official Django tutorial on their website and a guide on what need done specific to Hostgator.

The beginning of the tutorial worked just fine.  The first issue I had was getting a DoesNotExist error at ronaldsnyder.net/admin when I tried testing.  I found a post on stackoverflow that suggested logging into the intereactive prompt and recreating (in my case creating) the site through the intereactive shell.  

python manage.py
from django.contrib.sites.models import Site
Site.objects.create(pk=1, domain='www.ronaldsnyder.net', name='ronaldsnyder.net)

That solved that problem and now I have a working admin panel!  




I believe this problem was because the tutorial assumed you were working off from the built in test web server that comes with Django and not a real domain.  But wait....why isn't there any formatting or CSS?  It appears it is the way that Django deals with static files.  Some of this is handled by the development server but since I went with a live server right away I am running into some things not covered yet in the tutorials.  I am starting to think that I should have started with the dev server too....

After banging my head against the wall for a while I came up with this solution.  Find the css files included with django and find the path of the css.  So here is the path to the css when looking at the source:
href="/static/admin/css/base.css"
href="/static/admin/css/dashboard.css"

After some searching I found the media files at:

/usr/lib/python2.6/site-packages/django/contrib/admin/media which had the css, img and js directories.

I copied the css, img and js directories into ~/public_html/static/admin. BAM!!!! We have a formatted login page! I don't feel this is probably the correct way but it fixed my issue for now.






So the next thing I did was try to login to the admin panel but it seems we have another issue.  When I was doing the initial setup it appeared to me that the creation of the superuser failed but I continued on with the Django tutorial.  So now I try to create the superuser manually according to the Django documentation and I get TypeError: decode() argument 1 must be string, not None





I did some searching and found that this a bug from the locale not being set.  I was able to set the locale and fix the issue by running the following commands in the Hostgator session I had up:


export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

python manage.py createsuperuser --username=USERNAME

I have made it through page one of the Django tutorial and brushed upon the 2nd and it has taken me a lot more time than I expected. I thought I would get through the entire tutorial today. I have found Django a bit overwhelming so far but not unexpected.

**Edit February 2nd, 2013.  I would not recommend doing it this way.  I would set this up on my your local computer first and do the testing.  I would also use Eclipse with the PyDev plug in.  Take a look at http://ronsnyder.blogspot.com/2013/02/using-pydev-for-django.html for more information on why.