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

Monday, July 30, 2012

Handy trick for using django runserver/testmaker on remote server

Thanks Bart for showing me how to have a local version of django running on my VPS.

On the VPS start django as required, eg

python manage.py runserver
python manage.py testmaker -a web

Now do this from a command line where 1.2.3.4 is your vps ip:

ssh root@1.2.3.4 -L 5984:localhost:8000 -N

enter the password and it should not come back with anything.

Now go to this URL and your website should start

http://localhost:5984/

Tuesday, January 31, 2012

Customising django registration - no activation email or just a username

A client had a need for removing the email activation from the standard django registration process and a prototype system I am working on needed the ability to create a user from just a username, so I was delighted to find that the current django-registration development version makes this kind of customisation easy.

Here's how:

In the registration folder, there is a new folder called backends. Create a new folder, for example myreg, and put two files in it.

The __init__.py does the main work:

In this example there are two changes from the default, there is no email activation required and the registration form also asks for the users first and last names.

__init__.py


from django.conf import settings
from django.contrib.auth import authenticate
from django.contrib.auth import login
from django.contrib.auth.models import User
from django import forms

from registration import signals
from registration.forms import RegistrationForm


class MyRegBackend(object):
"""
A registration backend which implements the simplest possible
workflow: a user supplies a username, email address and password
(the bare minimum for a useful account), and is immediately signed
up and logged in.

"""
def register(self, request, **kwargs):
"""
Create and immediately log in a new user.

"""
username, email, password, first_name, last_name = kwargs['username'], kwargs['email'], kwargs['password1'], kwargs['first_name'], kwargs['last_name']

u = User.objects.create_user(username, email, password)
u.first_name = first_name
u.last_name = last_name
u.save()

new_user = authenticate(username=username, password=password)

login(request, new_user)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user

def activate(self, **kwargs):
raise NotImplementedError

def registration_allowed(self, request):
"""
Indicate whether account registration is currently permitted,
based on the value of the setting ``REGISTRATION_OPEN``. This
is determined as follows:

* If ``REGISTRATION_OPEN`` is not specified in settings, or is
set to ``True``, registration is permitted.

* If ``REGISTRATION_OPEN`` is both specified and set to
``False``, registration is not permitted.

"""
return getattr(settings, 'REGISTRATION_OPEN', True)

def get_form_class(self, request):

class MyRegForm(RegistrationForm):

"""
add first and last names to the form
"""
first_name = forms.CharField(
label='First name',
max_length=30,
required=True)
last_name = forms.CharField(
label='Last name',
max_length=30,
required=True)


return MyRegForm

def post_registration_redirect(self, request, user):
"""
After registration, redirect to the home page

"""

return ("/", (), {})

def post_activation_redirect(self, request, user):
raise NotImplementedError


The second file is urls.py where you need to point to your new backend

urls.py


from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

from registration.views import activate
from registration.views import register


urlpatterns = patterns('',
url(r'^register/$',
register,
{'backend': 'registration.backends.myreg.MyRegBackend'},
name='registration_register'),
url(r'^register/closed/$',
direct_to_template,
{'template': 'registration/registration_closed.html'},
name='registration_disallowed'),
(r'', include('registration.auth_urls')),
)



Now an even simpler version. Just enter a username and the user is created and you are logged in. Note that I wanted a default password so I made password1 a hidden field on the form with the value I wanted so that is how it is able to create the user. I could also have hard coded it into the register method below.


__init__.py


from django.conf import settings
from django.contrib.auth import authenticate
from django.contrib.auth import login
from django.contrib.auth.models import User

from registration import signals
from registration.forms import RegistrationForm


class MyReg2Backend(object):
"""
A registration backend which implements the simplest possible
workflow: a user supplies a username, email address and password
(the bare minimum for a useful account), and is immediately signed
up and logged in.

"""
def register(self, request, **kwargs):
"""
Create and immediately log in a new user.

"""
username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
User.objects.create_user(username, email, password)

# authenticate() always has to be called before login(), and
# will return the user we just created.
new_user = authenticate(username=username, password=password)
login(request, new_user)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user

def activate(self, **kwargs):
raise NotImplementedError

def registration_allowed(self, request):
"""
Indicate whether account registration is currently permitted,
based on the value of the setting ``REGISTRATION_OPEN``. This
is determined as follows:

* If ``REGISTRATION_OPEN`` is not specified in settings, or is
set to ``True``, registration is permitted.

* If ``REGISTRATION_OPEN`` is both specified and set to
``False``, registration is not permitted.

"""
return getattr(settings, 'REGISTRATION_OPEN', True)

def get_form_class(self, request):
return RegistrationForm

def post_registration_redirect(self, request, user):
"""
After registration, redirect to the user's account page.

"""
return (user.get_absolute_url(), (), {})

def post_activation_redirect(self, request, user):
raise NotImplementedError



Now in urls.py you just need to call your new backend

urls.py


from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

from registration.views import activate
from registration.views import register


urlpatterns = patterns('',
url(r'^register/$',
register,
{'backend': 'registration.backends.myreg2.MyReg2Backend', 'template_name': 'registration/registration_form_quick.html'},
name='registration_register'),
url(r'^register/closed/$',
direct_to_template,
{'template': 'registration/registration_closed.html'},
name='registration_disallowed'),
(r'', include('registration.auth_urls')),
)

Sunday, April 12, 2009

Installing Pinax 0.7 on CentOS

So Easy!

Here is the complete list of commands I used to install in a pinax directory of /home

easy_install virtualenv
easy_install pip
cd /home
mkdir pinax
cd pinax
curl -o pinax-boot.py http://github.com/pinax/pinax/raw/master/scripts/pinax-boot.py
python pinax-boot.py pinax-env
cd pinax-env
source bin/activate
pip install --requirement src/pinax/requirements/external_apps.txt
cd /home/pinax/pinax-env/src/pinax/pinax/projects
pinax-admin clone_project basic_project myproject
cd myproject

amend settings.py to point to required database - I'm using mysql
setup mysql database
python manage.py syndb
python manage.py runserver

DONE!

Monday, September 29, 2008

Playing with Django and Pinax - Mac OSX/MySQL

Installing Django - just followed instructions on django site http://docs.djangoproject.com/en/dev/intro/install/

Then ran through the tutorials

Then installed pinax using svn command here http://code.google.com/p/django-hotclub/source/checkout

Tried synchdb but didn't have PIL installed so got a load of errors.

Didn't have PIL so followed instructions here: http://www.p16blog.com/p16/2008/05/appengine-installing-pil-on-os-x-1053.html

setup data tables using
python ./manage.py syncdb

Wanted to use mysql - so installed ez_install
- downloaded egg here http://pypi.python.org/pypi/setuptools
- ran: sh setuptools-0.6c9-py2.5.egg

easy_install mysql-python


Then started mysql and created a new database and changed settings.py to use mysql and point to new database.

now syncdb again and tables created in mysql!
run: ./manage.py runserver
and pinax app available at http://127.0.0.1:8000

Wow!