@login_required
def message_create(request, content_type_id=None, object_id=None, 
                    template_name='messages/message_form.html'):
    """
    Handles a new message and displays a form.

    Template:: ``messages/message_form.html``
    Context:
        form
            MessageForm object
    """
    next = request.GET.get('next', None)
    if request.GET.get('to', None):
        to_user = get_object_or_404(User, username=request.GET['to'])
    else:
        to_user = None
    
    if content_type_id and object_id:
        content_type = ContentType.objects.get(pk=base62.to_decimal(content_type_id))
        Model = content_type.model_class()
        try:
            related_object = Model.objects.get(pk=base62.to_decimal(object_id))
        except __HOLE__:
            raise Http404, "The object ID was invalid."
    else:
        related_object = None
    
    form = MessageForm(request.POST or None, initial={'to_user': to_user})
    if form.is_valid():
        message = form.save(commit=False)
        if related_object:
            message.object = related_object
        message.from_user = request.user
        message = form.save()
        return HttpResponseRedirect(next or reverse('messages:messages'))
    return render_to_response(template_name, {
        'form': form,
        'to_user': to_user,
        'related_object': related_object,
        'next': next,
    }, context_instance=RequestContext(request))
dataset/ETHPy150Open nathanborror/django-basic-apps/basic/messages/views.py/message_create