Posts Tagged ‘python’

snippet : Generate n Length Random String using Python

I always forget stuff, so I’ll start to post my snippet here.

Generate n length random string

1
2
3
4
5
6
import random
 
def random_string(n):
    """ Create n length random string """
    code = ''.join([random.choice('abcdefghijklmnoprstuvwyxzABCDEFGHIJKLMNOPRSTUVWXYZ0123456789') for i in range(n)])
    return code

How to Programmatically Post to Facebook Wall

If you need to automatically publish something on your facebook wall or facebook pages wall, you can follow these steps:

Get your Facebook App Id and App Secret

You can get them from your facebook app, if you don’t have any then you can go to this page https://developers.facebook.com/apps and create new application.

Grant permission to write to the wall

You need to make sure that your application has write permission to the wall. You can use this url to grant your application write permission.

http://www.facebook.com/connect/prompt_permissions.php?api_key=YOUR_FACEBOOK_APP_ID&v=1.0&next=http://www.facebook.com/connect/login_success.html?xxRESULTTOKENxx&display=page&ext_perm=publish_stream,offline_access&enable_profile_selector=1&profile_selector_ids=PROFILE_ID_OF_WALL

Continue reading »

Handling 404 Page on FeinCMS

If you add dynamic content such as FeinCMS navigation in your 404 page then you need custom 404 handler.

Create custom views for 404 page.

from django.shortcuts import render_to_response
from django.template import RequestContext
from feincms.module.page.models import Page
 
def page_not_found(request, template_name='404.html'): 
    page = Page.objects.best_match_for_request(request) 
    return render_to_response(template_name, {'feincms_page' : page}, context_instance=RequestContext(request))

In urls.py add the following line :

handler404 = 'cms.views.page_not_found'

That’s it.

How to Reset Django Admin Password

django

There is a time when I forgot my password for Django admin. You can create new one using createsuperuser, but if you want to reset your available account you can use django shell :

python manage.py shell

Now, get your account and set new password.

from django.contrib.auth.models import User
 
user = User.objects.get(username='admin')
user.set_password('new_password')
user.save()

That’s it, your new password will get you in.

You can see the other django tips and tricks.

Getting Started With Django-CMS

Django CMS is content management system built in Django. You can extend the CMS via plugins, and there are already some plugins/extensions you can use or you can write custom plugin/extension for your specific need.

I’ll walk you through from installing to writing your first page. So, let’s go.

Continue reading »

Dynamic Module Loading in Python from a Directory

I have a case which need dynamic module loading from a directory. I found solution from stackoverflow that work for me and decide to share it here.

Continue reading »

Django 101

djangoDjango has excellent documentation, and it’s great place to get started if you want to learn about Django framework. The second place maybe this great Django book.

I also found lot of django tutorials on other sites, but some of them are very old and they probably  don’t work with current Django stable version (1.1.1 ). So I decided to collect recent tutorials about Django.

I’ve compiled list of  Django tutorials and articles to help myself with Django framework, and I  hope the list will help you too.

Continue reading »

10 Django Apps I Can’t Live Without

djangoI love Django pluggable applications. Basically you just need to install the application, and you’re done. How many hours do you spend to create registration system for your new project? Well, in Django you just need to use django-registration, set up the urls and everything is up and running. In this way you can save time and focus to your business logic.

Continue reading »

[Link] MongoEngine

MongoEngine is a Document-Object Mapper for working with MongoDB from Python. It has similar API with Django ORM.

Links – Top 10 tips to a new django developer

New beginner gotcha, help yourself by reading this.
Top 10 tips to a new django developer

Python Reading List

python logoI love Python.

To help you save your time, here’s some great (free) books to get started with Python :

  1. Dive Into Python by Mark Pilgram
    Dive Into Python is available online It’s one of the best way to get started with Python.
  2. A Byte of Python by Swaroop C H
    A Byte of Python gives a tutorial or guide to the Python language for a beginner audience.
  3. Learning with Python 2nd Edition by Jeffrey Elkner, Allen B. Downey, and Chris Meyers
    Another free tutorial to get started with Python. Learning with Python is available to read online.

So, what are your waiting for?