Posts about Django

Avatar for vbabiy@howsthe.com

django.contrib.auth User object login

Published Feb. 1, 2011 by Vitaly Babiy

While working on the sign up process I noticed it takes to many steps for a user to login to their new account. Where I wanted to get to was once the user completes the sign up process they're automatically redirected in to their account, and logged in. In order to make this work with django.contrib.auth it requires us to write a custom auth backend. Due to the clean api this is very trivial to do. Here is what my auth backend looks like:

from django.contrib.auth.backends import ModelBackend

class UserObjectAuthBackend(ModelBackend):
    def authenticate(self, username=None, password=None, user=None):
        if not user:
            try:
                user = self.user_class.login.get(username=username)
            except self.user_class.DoesNotExist:
                pass

            if user and user.check_password(password):
                return user
            return None
        return user

If you‘ve ever created a auth backend you will notice something a little different about my version, I will explain this a little later. First a little explanation of what the backend is doing. In order to implement a backend we will need to over write one method, authenticate. This method is suppose to take a username and password, and using this information it needs to look up the user and then verify that the password is correct, if its correct return the user object. Our goal is to login a user using only the User object, to reach our goal we will change the default api just a little to allow us to pass in a user object to authenticate. In this version the only time we need to check the users password is when we don’t already have a user object, and if we do we just return it. To wrap up the work on the auth backend we will need to let the auth app we want to use our custom backend, to do this we need to add the following to our settings.py file:

AUTHENTICATION_BACKENDS = (
    'lib.UserObjectAuthBackend',
)

Now its time to see the view that will login in a user and redirect them to where ever they need to be:

from django.contrib.auth import authenticate, login

def signup(request):
    # ...
    # Sign up process
    # `user` is the new user that just signed up.
    # ...
    user = authenticate(user=user)
    login(request, user)
    return HttpResponseRedirect('/my_account')

That’s it folks, when the user see the next page they will be logged in. For more information also check out the great Django documentation. If you got any question or suggestions leave them below.

Tags
  • auth
  • Django
Avatar for vbabiy@howsthe.com

Python mocking with mox

Published Jan. 10, 2010 by Vitaly Babiy

When building an application it's hard not to depend on some outside resource, that you have no control over. When writing unit tests for this type of application it gets tricky, but thanks to mocking you are able to do so. Mocking comes in two forms, manual (a.k.a monkey patching) or using a mocking framework, we will be using the latter. A mocking framework creates objects that add expectations, define their methods, and return values for each method call in a simple way.

There are a few mocking frameworks available for python:

Avatar for vbabiy@howsthe.com

Django Testing

Published Jan. 4, 2010 by Vitaly Babiy

Test driven development will help in a few aspects when developing an application. One aspect is you get to eat your own dog food even before you make it. You are able to use your API's before they are implemented, this will display any short comings in the design. Also by forcing yourself to think through a problem and get a better understanding, which in turn helps make better design decisions. We all make mistakes, without testing our code base there is no way to be sure that everything is still working, otherwise you are crossing your fingers and knocking on wood.

Django and Python Test Tools

If you've been using Django for some time you have heard the phrase "Django is just Python" and this holds true in the realm of testing your Django Application. You can use other testing frameworks with Django like nose. Django comes with a good testing framework that is built off the Python unittest module. The default Django testcase is a subclass of unittest.TestCase this class provides with some additional assertions and also a client that will helps you test your views ( more details to come ).

Avatar for vbabiy@howsthe.com

Django with nginx, mod_wsgi, and SSL

Published Sept. 20, 2009 by Vitaly Babiy

Howsthe.com has made an architectural change in our deployment stack. We were using mod_wsgi and Apache to serve the static content, which was not working as well as planned. We moved to using nginx as a front end proxy and a static content server. It also acts as a gateway for the SSL connection so we don't have to run different mod_wsgi instances for HTTP connections and for HTTPS connections. Below we explain how we configured this setup.

Prerequisites:

We'll be using Ubuntu 9.04 server, the following packages need to be installed:

  • apache2
  • libapache2-mod-wsgi
  • nginx
  • subversion ( Only if you are going to install Django from SVN )

This doesn't include database installation, for this example we will not be using a database. If your application uses a database, install the correct python drivers and make sure your settings.py file is configured correctly.

Once these packages are installed, then install Django. We will not be going over this here due to expediency. You can find the directions at http://docs.djangoproject.com/en/dev/intro/install/.

Avatar for vbabiy@howsthe.com

12 Tips to Better SEO

Published Sept. 15, 2009 by Vitaly Babiy

Advertising is very expensive, studies have shown that it is not as effective as being on the first page of search results. These SEO tips will help you rank higher on the search engines results, and who doesn't like free referrals.

  1. Content is King
    Search engines want something to index. If your website doesn't have much content the search engine will not rank you as higher authority for these keywords. Also it gives other websites the opportunity to link to you. For instance if you have a blog, this can generate many links to your website. These links will allow the page rank to flow, the more links to your website the sooner it will be indexed and more often.

  2. SEO Friendly URLs
    URLs that use query parameters to get a website like http://www.howsthe.com/blog?id=100, have nothing in the URL that describes what the website is about. On the other hand a URL similar to http://www.howsthe.com/blog/12-seo-tips-for-beginners/ gives a brief description of the content on the page.
    It's important to optimize your website for keywords that match what your user base is searching for. Using URLs with less query parameters allows these keywords to be placed within the URL.

A blog about development, marketing, and design.

The next version of Ubuntu is coming soon