Django Tutorial – Building URL Shortener with Django

Tags

, ,

I finally got the time to write a Django tutorial. In this tutorial we are building URL Shortener. If you are confused with what we are building then we are building something similar to (bit.ly) or (goo.gl).

We are building the app that will make a shorter URL of a longer URL. (Example) Normally the shorter URL will be localhost:8000/4hh5kr for http://www.facebook.com

I am using Django 1.7.1 with Python 3.3.2+. Don’t worry if you have different version of Django because we will set up our virtual environment and install latest version of Django. But be sure you do have Python 3+ because installing Python will not be covered in this tutorial.

I assume that you have completed Writing your first Django app. It is not really essential to complete this tutorial but it is highly recommended that you complete this tutorial before we begin. It will give you a lot of knowledge that will be useful in this tutorial.

You can also get the source code of this app in Github(django_urlshortener).

This tutorial is divided into 5 parts:

Continue reading

Move to Next Activity in AndEngine (Android)

Tags

I was developing the game using AndEngine. I had two activity Welcome screen(Welcome.java) and Game Screen(Game.java) when user clicks on Start button in Welcome Screen he should be moved to next activity.

I end up with the following code. You can also use the following code to change the activity in AndEngine.

Place this code somewhere on suitable event such as onClick() event after which you want to change the activity.

Welcome.this.startActivity(new Intent(Welcome.this, Game.class));
Welcome.this.finish();

Change the class name to yours and fix the import Error that will occur.

And finally add this code in AndroidManifest.xml before </application>

<activity android:name="com.example.myGame.Game"> </activity>

Don’t forget to change your package name and Class name.

E: Sub-process /usr/bin/dpkg returned an error code (1)

Tags

I recently ran into the following error in Ubuntu (v 13.10).

E: Sub-process /usr/bin/dpkg returned an error code (1)

Due to this error i was not able to run the update. Here is the fix to this error.

First run:

$ sudo apt-get remove install-info

Then run this command to clear apt archives:

$ sudo apt-get clean

Then finally run

$ sudo apt-get update
$ sudo apt-get upgrade

How to remove /public/ from URL in Laravel

Tags

By default in Laravel the URL to access your site is http://example.com/public/. It is not good to keep public in URL as it makes URL ugly and longer.

So, let’s talk about the solution to remove /public/ from the URL.

Method I : Using .htaccess
Create a .htaccess file your Laravel root directory if it does not exists already. (Normally it is under your public_html folder)

And add the following code to it:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Now you can access your site via http://example.com.

Method II : Moving the contents of public folder to root directory
Actually i prefer this method because the above method is not so good to use in production environment.

So now lets make a new folder laravelfiles in your root directory and move all the files and folder except public directory to laravelfiles.

And move everything of public directory to root folder. Now your root directory will look something like this:

Remove Laravel Public url

Now we have to change the paths in laravelfiles/bootstrap/paths.php file and index.php.

Find these lines in laravelfiles/bootstrap/paths.php

    'app' => __DIR__.'/../app',
    'public' => __DIR__.'/../public',

Change these two lines to:

    'app' => __DIR__.'/../app',
    'public' => __DIR__.'/../../',

Find these lines in index.php

    require __DIR__.'/../bootstrap/autoload.php';

    $app = require_once __DIR__.'/../bootstrap/start.php';

And change to:

    require __DIR__.'/laravelfiles/bootstrap/autoload.php';

    $app = require_once __DIR__.'/laravelfiles/bootstrap/start.php';