Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Feb 16, 2013

Heroku Error H14 with Django

H14 Error on Heorku


Today morning I did a new push to www.bikenomads.co.in with images from the fantastic 2013 calendar.

And the website died :(

One minute it was working fine, and the next it died ! Showing a problem with the app, and if you are the administrator to check the logs.

It took me 2 hrs to fix this, as Heroku site is not clear on what to do in case of errors ( see here for list of errors in Heroku) . So, here's a short guide to fix it ( this guide is for a Django project but I think it should work out similarly for other platforms )

First , if the app is not loading , check the logs in heroku. So, I logged into the web console and could not find the error log at all ! Apparently you need to use the command line for the logs as :

$ heroku logs
and it showed something like this to me : 
2013-02-16T08:35:49+00:00 heroku[web.1]: Process exited with status 143
2013-02-16T08:35:51+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path=/ host=www.bikenomads.co.in fwd="122.161.143.22" dyno= queue= wait= connect= service= status=503 bytes=

The what ?! Web process exited ?? Then how will the site run ? I checked the web console and it did not show anything under "Dynos" ( I am also not sure if it existed 2 months back?) 

Check with heroku ps if there is any process running: 
$ heroku ps
That did not return anything. 

So, a bit of search over the net and stackexchange pointed to something called the Procfile - which I had never used before. Apparently, heroku picks up the info on what to run automatically, but sometimes it jinxes. It is safer to put the information into a Procfile . This problem seems to keep recurring for over a year, but Heroku guys have not fixed it or fixed their tutorials, and that is where it all starts going downhill. 

Now follow the steps below to fix it for your installation : 
1. Create a file called Procfile ( it is case sensitive - so capitalize P !) in the root directory of your application ( where you have the venv folder ) . On windows you will have to make sure that windows does not add a .txt extension . I use linux so it was just 'touch Procfile'.
2. Edit the file and put in the following details for Django ( other apps you will need to figure out depending on what is needed to run it ) :
web: python website/manage.py runserver 0.0.0.0:$PORT
Note the spacing and the IP you need to give. If you miss out the last part, it will run on localhost which you will not be able to connect to from your machine. 
3. git add the file and do a git push heroku master to push this file to the server. We are not done yet even though the push says everything is fine.
4. Remember the heroku ps did not give any results earlier ? So, now you need to attach the process. Do this with the following command to add a web dyno :
$ heroku ps:scale web=1
Scaling web processes... done, now running 1
Check your website now and it should be working ! 

And check out my site : http://www.bikenomads.co.in 

Apr 1, 2012

Developing Django app on both Windows and Linux

I was doing most of my development on Linux so far. My desktop is dual boot, so I can log into linux or Windows at whim. This is a small inconvenience. However the bigger problem is when I am travelling as my laptop is by necessity a Windows one. This problem occupied my mind this weekend and the following is how I am getting it to work.

First I used dropbox (in case you want dropbox, please drop me a line as referrals get me more space :) ) and installed on both Windows and Linux. Next step was to move the folder where I was developing into Dropbox. This helps immensely as I dont have to worry while travelling and my code is always replicated into the 3 locations that I work in.

Next I setup git - this is an optional step for you, but I found that it helps that even on my local projects I have version system to go and look back at what changes I have made.

Now the things were working on Linux ( you can look at my earlier posts about the other problems that I had ), so I logged into windows and tried to work out the problems.

On Windows to use Django I use Bitnami Django Stack. It is quite quick and painless to use. The only problem is that the mysql install does not allow you to have root password to have special characters - which is quite common for me. The work around is to install with normal password and then start the django stack and use the mysqladmin to change the root password. I went through all this because I use root for django settings. Yeah, I know i should change it - and will probably do later when I am ready to deploy.

Next on the Bitnami shell do a syncdb and runserver.

On navigating to the http://127.0.0.1:8000 URL, I got a template not found error for index.html

Looking at the settings.py file I found the culprit immediately. The settings for Dirs was very linux specific


TEMPLATE_DIRS = (
"/home/vibhu/website/templates",
)

Same was for the STATIC_DIRS settings.

The solution to this is not to replace it with windows directory as that will just mean that when I log into linux I will again get the problem and have to change back. Searching through the django site and internet I finally got the right solution ( some books have it wrong and the examples were not working). The following seems to work for me:

Edit the settings.py file with the following somewhere at the top



#hack to accomodate Windows
import os
CURRENT_PATH=os.path.abspath(os.path.dirname(__file__))



And then in the relevant sections change the path lines :


STATICFILES_DIRS = (
os.path.join(CURRENT_PATH,'static'),
)
TEMPLATE_DIRS = (
os.path.join(CURRENT_PATH,'templates'),
)



Make sure that you have the trailing comma else the admin login view gets screwed.

Simple enough when you know it.

Mar 28, 2012

Login URLs in Django

Django is a pain.

I know - its a great framework. Its also supposed to be robust and highly recommended by a few friends when I asked around for what should I use as I wanted to learn how to develop a website.

I have been dilly-dallying with making the website for quite some time, but now I am trying to put in a few hrs every week into it. I enjoy it so far... but then I run into some stupid roadblock.

Today I ran into another one. If I do not document it, I am sure that I will make the same mistakes again.

Today's problem was with the login redirect URL.

I have a function with the decorator as follows :

@login_required
def foo(request, template_name="foo.html"):
    return render_to_response(template_name,locals(),context_instance=RequestContext(request))

I changed the default account redirect to /account so my urls.py has the following:
    (r'^account/', include('account.urls')),

Now, when I tried to click on the link which requires the login ( and I am not logged in) I got the following error :

The current URL, accounts/login/, didn't match any of these.

See the s in accounts. I tried searching everywhere where I had the accounts word. grep did not turn up anything and I was quite frustrated.

Finally, I reasoned that it has to be something with the way django does anything and started reading the settings documentation. Sure enough, @login_required requires a url which defaults to /accounts/login !

Why the Django documentation has to be so vague with login is something I don't understand.

So, I added the following lines in the settings.py :
LOGIN_URL='/account/login/'

Not it works !

Gah !